我有一個隨機格式的蘋果產品名稱串列。比如取一個iphone 11 pro可以找到的產品名稱
Iphone 11 Pro或iphone 11 Pro,或任何可能的東西。但我想將其更改為蘋果給它們的命名模式,例如:iPhone 11 Pro
所以,我試圖先將所有內容更改為標題,然后替換字串的前兩個字符。但問題是第二部分不起作用。作為初學者,我無法重現該解決方案。我已閱讀有關 python 中的正則運算式的文章。但無法找到更好的方法來做到這一點。
我就是這樣努力的。。
names = ['Iphone 12', 'iphone 11 pro', 'IPad pro', 'Imac pro']
titled_names = []
updated_names = []
# first change all to title
for i in names:
i.title()
titled_names.append(i)
# replace the first two char
for i in titled_names:
i.replace('Ip', 'iP', 1)
updated_names.append(i)
print(updated_names)
但這不應該無論如何都可以作業,因為有些產品的第一個字符不會是 Ip,比如在 Imac 中。名稱串列的最終結果應該是這樣的。
names = ['iPhone 12', 'iPhone 11 Pro', 'iPad Pro', 'iMac Pro']
那么我怎樣才能做到這一點。第一個字符在第一個字母中小寫第二個大寫字母,其余在標題大小寫中
uj5u.com熱心網友回復:
您可以指定所需命名的串列。
# update to your needs
CORRECT_STRINGS = ["Apple", "iPhone", "iPad", "iMac", "MacBook", "Pro", "SE", "Max"]
names = ['Iphone 12', 'iphone 11 pro', 'IPad pro', 'Imac pro']
new_names = []
for name in names:
# convert to lower-case
correct_name = name.lower()
# replace all matching string to correct one
for correct_string in CORRECT_STRINGS:
# we want to find a lower-case string and replace it
match_string = correct_string.lower()
correct_name = correct_name.replace(match_string, correct_string)
# add correct device name to the result list
new_names.append(correct_name)
print(new_names)
>>> ['iPhone 12', 'iPhone 11 Pro', 'iPad Pro', 'iMac Pro']
雖然,如果某個名稱是另一個名稱的子字串,這種方法可能并不總是正常作業。對于 Apple 產品,情況可能并非如此。
更新:更優雅的解決方案(僅替換完全匹配的字串)
# update to your needs
DEVICE_NAME_STRINGS = [
"Apple",
"iPhone",
"iPad",
"iMac",
"MacBook",
"Pro",
"SE",
"Max",
]
DEVICE_NAME_STRINGS_MAP = {s.lower(): s for s in DEVICE_NAME_STRINGS}
names = ['Iphone 12', 'iphone 11 pro', 'IPad pro', 'Imac pro']
def standardize_device_name(device_name: str):
"""
Example: iPhOnE 13 PrO mAx -> iPhone 13 Pro Max
"""
return " ".join(
[
DEVICE_NAME_STRINGS_MAP.get(word.lower(), word)
for word in device_name.split()
]
)
new_names = [standardize_device_name(name) for name in names]
print(new_names)
uj5u.com熱心網友回復:
奇跡般有效!
def capitalize_nth(s, n):
"""This function will capitalize nth charcater in string"""
return s[:n].lower() s[n:].capitalize()
def edit_strings(name):
# convert given string to lowercase and strip any whitespace infront & back
name = name.lower().strip()
# split string based on first space, since we need to capitalize second character only for the first substring(product name)
# Eg: ['iphone', '12']
sub_strs = name.split(" ", 1)
return " ".join([
capitalize_nth(sub_strs[0], 1), # capitalize second character of product name
sub_strs[1].title() # capitalize first character of each word in string
])
names = ['Iphone 12', 'iphone 11 pro', 'IPad pro', 'Imac pro', 'iphone 12 pro max']
edited_names = [edit_strings(name) for name in names]
print(edited_names)
輸出:
['iPhone 12', 'iPhone 11 Pro', 'iPad Pro', 'iMac Pro', 'iPhone 12 Pro Max']
uj5u.com熱心網友回復:
我做了一些改變并使用了另一種方法。在這里,我撰寫了函式來獲取并回傳所有首字母小寫的串列成員。如果您還需要第二個字母,您也可以將第二個字母索引添加得更低(在評論中添加)。我猜這是初學者最簡單的方法。
def first_letter_to_lower(givenList):
for i in givenList:
i = i[0].lower() i[1::]
print(i)
first_letter_to_lower(names)
OUTPUT
iphone 12
iphone 11 pro
iPad pro
imac pro
如果你想恢復你的串列,你可以在函式中添加一個 append 方法以在“givenList”中回傳 i.lowwered
uj5u.com熱心網友回復:
您需要將字串方法回傳的值附加到您的串列中:
names = ['Iphone 12', 'iphone 11 pro', 'IPad pro', 'Imac pro']
titled_names = []
updated_names = []
# first change all to title
for i in names:
titled_names.append(i.title())
# replace the first two char
for i in titled_names:
updated_names.append(i.replace('Ip', 'iP', 1))
print(titled_names)
print(updated_names)
輸出:
['Iphone 12', 'Iphone 11 Pro', 'Ipad Pro', 'Imac Pro']
['iPhone 12', 'iPhone 11 Pro', 'iPad Pro', 'Imac Pro']
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473898.html
