【发布时间】:2019-01-25 06:23:00
【问题描述】:
请帮助我。到目前为止,我做到了。
stopwords=['what','hello','and','at','is','am','i','to','the']
search_list=['where is north and northern side','finding the index'
'ask in the community at western environmental',
'my name is alan and i am coming from london southeast','access to the index']
rev_dict ={'east': 'e', 'eastern': 'e', 'enviornment': 'env', 'environ.':
'env','environmental': 'env', 'north': 'n', 'northern': 'n', 'south': 's',
'southern': 's', 'west': 'w', 'western': 'w'}
result = [" ".join([rev_dict.get(x,x) for x in s.split() if x not in
stopwords]) for s in search_list]
print (result)
在运行上述代码时,它会给出以下输出
['where n n side', 'finding index','ask in community w env', 'my name alan coming from london southeast','access index']
帮助: 如果每个 search_list 字符串少于或等于 20 个字符(包括空格),则不应应用“停用词和 rev_dict”步骤并按原样打印字符串。如果每个字符串只有 20 个以上的字符,我应该同时应用 'stopwords 和 rev_dict' 步骤。
我应该如何更改上面的代码以获得我想要的以下输出。 'finding the index' 和 'access to the index' 必须按原样打印,因为它们少于或等于 20 个字符。
['where n n side', 'finding the index','ask in community w env', 'my name alan coming from london southeast','access to the index']
【问题讨论】:
标签: list dictionary split slice