首页 > 解决方案 > 将其他停用词附加到 nltk.corpus.stopwords.words('english') 列表或作为一组更新返回 NoneType 对象

问题描述

我尝试从 nltk 附加到停用词(作为列表和集合)。但是,它返回一个 NoneType 对象。我使用了以下方法:

  1. 扩展列表:

    stopword = list(stopwords.words('english'))

    stopword = stopword.extend(['maggi','maggie','#maggi','#maggie'])

    打印(停用词)

    没有任何

  2. 更新集合

    stopword = set(stopwords.words('english'))

    stopword = stopword.update(set(['maggi','maggie','#maggi','#maggie']))

    打印(停用词)

    没有任何

标签: pythonpython-3.x

解决方案


stopwords.words('english') 已经是一个列表,因此您无需再次转换为列表。在使用给出 None 类型输出的 list.extend() 的地方,我们可以创建另一个列表并将其添加到停用词中。因此,以下代码将完成任务并为您提供输出

from nltk.corpus import stopwords
stopword = list(stopwords.words('english'))
l = ['maggi','maggie','#maggi','#maggie']
stopword = stopword + l
print(stopword)

推荐阅读