【问题标题】:List of words with List Index带有列表索引的单词列表
【发布时间】: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


    【解决方案1】:
    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会解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2021-11-14
      • 2018-12-15
      相关资源
      最近更新 更多