Python 从类似字符串列表中获取后缀
嗨,伙计们,我有一个小问题要找到我的产品列表的后缀。我得到了公共前缀函数,它为我的列表提供了正确的答案,但是当我尝试删除每个列表元素的公共前缀时,它不起作用,你知道为什么吗? 多谢各位Python 从类似字符串列表中获取后缀,python,prefix,suffix,Python,Prefix,Suffix,嗨,伙计们,我有一个小问题要找到我的产品列表的后缀。我得到了公共前缀函数,它为我的列表提供了正确的答案,但是当我尝试删除每个列表元素的公共前缀时,它不起作用,你知道为什么吗? 多谢各位 您无需重新设计os.path.commonprefix 此外,您可以对字符串使用语法,而不是逐个计算字符 解决方案 # code to get commonprefix def commonprefix(m): if not m: return '' s1 = min(m)
os.path.commonprefix
李>
# code to get commonprefix
def commonprefix(m):
if not m: return ''
s1 = min(m)
s2 = max(m)
for i, c in enumerate(s1):
if c != s2[i]:
return s1[:i]
return s1
#code to get the different suffix
strList = map(str, objList)
strList = map(lambda x: x.replace('', ''), strList)
# My code to get the different suffix of each element of a list
for i in range(len(liste)):
Listelement = liste[i]["title"].tolist()
common_name = commonprefix(Listelement)
try:
strList = map(str, Listelement)
strList = map(lambda x: x.replace(common_name, ''), strList)
print(Listelement)
except:
pass
# this is how my "Listelement" variable look like
['Le Coton Coton Extra Doux Boîte 100pce - CHANEL']
['Allure Eau De Toilette Vaporisateur 50ml - CHANEL', 'Allure Eau De Toilette Vaporisateur 100ml - CHANEL']
['Allure Eau De Toilette Vaporisateur 50ml - CHANEL', 'Allure Eau De Toilette Vaporisateur 100ml - CHANEL']
['Eau De Cologne Les Exclusifs De Chanel 75ml - CHANEL', 'Eau De Cologne Les Exclusifs De Chanel 200ml - CHANEL']
['Eau De Cologne Les Exclusifs De Chanel 75ml - CHANEL', 'Eau De Cologne Les Exclusifs De Chanel 200ml - CHANEL']
os.path.commonprefix
李>
# code to get commonprefix
def commonprefix(m):
if not m: return ''
s1 = min(m)
s2 = max(m)
for i, c in enumerate(s1):
if c != s2[i]:
return s1[:i]
return s1
#code to get the different suffix
strList = map(str, objList)
strList = map(lambda x: x.replace('', ''), strList)
# My code to get the different suffix of each element of a list
for i in range(len(liste)):
Listelement = liste[i]["title"].tolist()
common_name = commonprefix(Listelement)
try:
strList = map(str, Listelement)
strList = map(lambda x: x.replace(common_name, ''), strList)
print(Listelement)
except:
pass
# this is how my "Listelement" variable look like
['Le Coton Coton Extra Doux Boîte 100pce - CHANEL']
['Allure Eau De Toilette Vaporisateur 50ml - CHANEL', 'Allure Eau De Toilette Vaporisateur 100ml - CHANEL']
['Allure Eau De Toilette Vaporisateur 50ml - CHANEL', 'Allure Eau De Toilette Vaporisateur 100ml - CHANEL']
['Eau De Cologne Les Exclusifs De Chanel 75ml - CHANEL', 'Eau De Cologne Les Exclusifs De Chanel 200ml - CHANEL']
['Eau De Cologne Les Exclusifs De Chanel 75ml - CHANEL', 'Eau De Cologne Les Exclusifs De Chanel 200ml - CHANEL']
你能举一个最小的可复制的例子吗?在这里,我们不知道如何使用您的数据,也不知道您所列出的是什么,您能否明确举例说明您对“公共前缀”和“公共后缀”的定义?特别是对于后缀,它应该是
ml-CHANEL
还是CHANEL
?例如,对于第二个列表,后缀应该是['50ml-CHANEL','100ml-CHANEL'],我只想删除类似的部分。你能做一个最小的可复制示例吗?在这里,我们不知道如何使用您的数据,也不知道您所列出的是什么,您能否明确举例说明您对“公共前缀”和“公共后缀”的定义?特别是对于后缀,如果是ml-CHANEL
或CHANEL
?例如,对于第二个列表,后缀应该是['50ml-CHANEL','100ml-CHANEL'],我只想删除类似的部分