3 回答

TA贡献1963条经验 获得超6个赞
我建议您使用以下解决方案,使用re.subwithflags=re.IGNORECASE去除忽略大小写的常用词:
import re
city = ['Venango Municipality', 'Waterford ship','New York']
common_words = ['ship','municipality']
toAppend = []
for c in city:
for cw in common_words:
if cw.lower() in c.lower().split():
toAppend.append(re.sub(cw, "", c, flags=re.IGNORECASE).strip())
city += toAppend
print(city) # ['Venango Municipality', 'Waterford ship', 'New York', 'Venango', 'Waterford']
这是一个使用列表理解的单线样式解决方案,虽然简短,但可读性较差:
import re
city = ['Venango Municipality', 'Waterford ship','New York']
common_words = ['ship','municipality']
city += [re.sub(cw, "", c, flags=re.IGNORECASE).strip() for c in city for cw in common_words if cw.lower() in c.lower().split()]
print(city) # ['Venango Municipality', 'Waterford ship', 'New York', 'Venango', 'Waterford']

TA贡献1811条经验 获得超5个赞
您可以尝试一下,创建新列表以保存数据,应将数据添加到原始列表中,然后合并结果:
In [1]: city=['Venango Municiplaity', 'Waterford ship','New York']
In [2]: common_words = ['ship', 'municiplaity']
In [3]: list_add = []
In [4]: for item in city:
...: item_words = [s.lower() for s in item.split(' ')]
...: if set(common_words) & set(item_words):
...: new_item = [s for s in item.split(' ') if s.lower() not in common_words]
...: list_add.append(" ".join(new_item))
...:
In [5]: city + list_add
Out[5]: ['Venango Municiplaity', 'Waterford ship', 'New York', 'Venango', 'Waterford']
添加回答
举报