在python列表中替换字符串中的字符



我有一个随机格式的苹果产品名称列表。例如单个产品名称iphone 11 pro,可以找到

Iphone 11 Proiphone 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']

那么我怎么才能做到这一点。第一个字符小写第一个字母大写,其余字母用标题大小写

您可以指定所需的命名列表。

# 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']

但是,如果某个名称是另一个名称的子字符串,则此方法可能并不总是正确工作。如果是苹果的产品,情况可能就不一样了。

UPDATE:一个更优雅的解决方案(只替换完全匹配的字符串)

# 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)

效果非常好!

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']

我做了一些更改,并使用另一种方法。这里我写了一个函数来获取并返回所有首字母为小写的列表成员。如果您还需要第二个字母,您可以添加第二个字母索引,以及更低(在评论中添加)。我想这是初学者最简单的方法。

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方法来返回i. lowered in "givenList">

您需要将字符串方法返回的值附加到列表中:

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']

最新更新