List of words with List Index

时间:2018-08-18 12:00:23

标签: list dictionary split slice

Kindly help me on that. so far I did.

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)

while run the above codes it gives the below output

['where n n side', 'finding index','ask in community w env', 'my name alan coming from london southeast','access index']

Help: If each search_list string has less than or equal to 20 characters(including space) then it should not apply the 'stopwords and rev_dict' steps and print the string as it is. If each string has above 20 characters only I should apply both 'stopwords and rev_dict' steps.

What should I change the above code to get my below desirable output. 'finding the index' and 'access to the index' has to print as it is because they have less than or equal to 20 characters.

['where n n side', 'finding the index','ask in community w env', 'my name alan coming from london southeast','access to the index']

1 个答案:

答案 0 :(得分:0)

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]) if len(s) > 20 else s for s in search_list]

print (result)

输出:

['where n n side', 'finding the index', 'ask in community w env', 'my name alan coming from london southeast', 'access to the index']

使用if len(s) > 20将解决此问题。

相关问题