【问题标题】:Removing stopwords from list of lists从列表列表中删除停用词
【发布时间】:2020-12-22 18:20:32
【问题描述】:

我想知道如何从如下列表中删除特定单词,包括停用词:

my_list=[[],
 [],
 ['A'],
 ['SB'],
 [],
 ['NMR'],
 [],
 ['ISSN'],
 [],
 [],
 [],
 ['OF', 'USA'],
 [],
 ['THE'],
 ['HOME'],
 [],
 [],
 ['STAR'],
 []]

如果它是一个字符串列表,我会应用如下内容:

from collections import Counter
stop_words = stopwords.words('english')
text = ' '.join([word for word in my_list if word not in stop_words])

我需要在最后做这样的事情:

counts= Counter(chain.from_iterable(my_list))
plt.bar(*zip(*counts.most_common(20)))
plt.show()

预计要绘制的列表:

my_list=[[],
 [],
 ['SB'],
 [],
 ['NMR'],
 [],
 ['ISSN'],
 [],
 [],
 [],
 ['USA'],
 [],
 ['HOME'],
 [],
 [],
 ['STAR'],
 []]

【问题讨论】:

  • 那么预期的输出是什么? Counter 与删除单词有什么关系?
  • 我从列表中删除了计数器。现在可能它应该更好。我添加了一个输出示例(用于列表),以便绘制它

标签: python stop-words


【解决方案1】:

循环通过my_words,将每个嵌套列表替换为删除停用词的列表。您可以使用设置差异来删除单词。

stop_words = stopwords.words('english')
my_list = [list(set(sublist).difference(stop_words)) for sublist in my_list]

不区分大小写的比较有点复杂,因为您不能使用内置的集差法。

my_list = [[word for word in sublist if word.lower() not in stop_words] for sublist in my_list]

【讨论】:

  • 谢谢巴马尔。我试过了,但是在 my_list 中仍然有一些像 the, of 这样的停用词,所以当我绘制时我仍然看到它们。
  • 这可能是因为停用词是小写的,但您的列表包含大写的词。
  • 你能把my_list改成全小写吗?如果没有,我展示了如何在转换大小写后进行比较。
  • 这是有道理的。谢谢巴尔玛!
猜你喜欢
  • 2018-09-28
  • 2021-12-22
  • 2021-02-02
  • 2016-09-25
  • 2019-09-10
  • 1970-01-01
  • 2021-02-17
  • 2017-06-21
  • 2015-05-30
相关资源
最近更新 更多