替换 python 列表中字符串中的字符
Replacing characters from string in python list
我有一个随机格式的苹果产品名称列表。
比如取一个单品名iphone 11 pro
可以找到
Iphone 11 Pro
或 iphone 11 Pro
或任何可能的值。
但我想将其更改为苹果如何给它们命名的模式,例如:iPhone 11 Pro
因此,我尝试将所有 then first 更改为 title,然后替换字符串的前两个字符。但问题是第二部分不起作用。作为初学者,我无法重现该解决方案。我已经阅读了 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']
那么我怎样才能做到这一点。
first char small second capital in first letter,其余在Title Case
您需要将字符串方法返回的值附加到您的列表中:
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']
我做了一些更改并使用了另一种方法。在这里,我写了一个函数来获取和 return 所有列表成员,首字母小写。如果您还需要第二个字母,您也可以添加第二个字母索引(在评论中添加)。我想这对初学者来说是最简单的方法。
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方法到return i.lowwered in "givenList"
您可以指定所需命名的列表。
# 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)
很有魅力!
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']
我有一个随机格式的苹果产品名称列表。
比如取一个单品名iphone 11 pro
可以找到
Iphone 11 Pro
或 iphone 11 Pro
或任何可能的值。
但我想将其更改为苹果如何给它们命名的模式,例如:iPhone 11 Pro
因此,我尝试将所有 then first 更改为 title,然后替换字符串的前两个字符。但问题是第二部分不起作用。作为初学者,我无法重现该解决方案。我已经阅读了 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']
那么我怎样才能做到这一点。 first char small second capital in first letter,其余在Title Case
您需要将字符串方法返回的值附加到您的列表中:
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']
我做了一些更改并使用了另一种方法。在这里,我写了一个函数来获取和 return 所有列表成员,首字母小写。如果您还需要第二个字母,您也可以添加第二个字母索引(在评论中添加)。我想这对初学者来说是最简单的方法。
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方法到return i.lowwered in "givenList"
您可以指定所需命名的列表。
# 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)
很有魅力!
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']