【发布时间】:2019-01-18 23:53:12
【问题描述】:
请帮帮我。
- 我有一个停用词列表和一个搜索列表。我想删除那些 搜索列表中的停用词。
- 在(第 1 步)之后,我想将每个拆分词与字典值进行匹配。如果 值匹配将特定单词替换为相应的字典键 然后加入其他单词。
到目前为止,我已经完成了第 1 步(参见下面的代码)。效果很好:
stopwords=['what','hello','and','at','is','am','i']
search_list=['where is north and northern side',
'ask in the community at western environmental',
'my name is alan and i am coming from london southeast']
dictionary = {'n': ['north','northern'],
's': ['south','southern'],
'e': ['east','eastern'],
'w': ['west','western'],
'env': ['environ.','enviornment','environmental']}
result = [' '.join(w for w in place.split() if w.lower() not in stopwords)
for place in search_list]
print (result)
我需要以下理想的最终输出来完成步骤 2。为了获得我想要的最终输出,我应该在上面的一行代码中更改/包含什么?也欢迎任何其他替代方法。
['where n n side', 'ask in the community w env', 'my name alan coming from london s']
【问题讨论】:
-
如果你在字典中反转你的键/值会更清楚,所以
{ 'north': 'n', 'northern': 'n' ... }等。它也会让你的代码更容易维护。
标签: python list dictionary replace split