如何计算单词出现在python列表中的次数

How to count how many times a word appears in a list in python

本问题已经有最佳答案,请猛点这里访问。

我有下面的python列表

1
2
3
4
5
6
texts = [
    ["great","even","for","the","non","runner","this","sound",
    "track","was","brilliant"],
    ["cannot","recommend","as","a","former","comrade","i","did",
    "not","want","to","have","to","do","this"]
]

我想浏览一下列表,计算每个单词在其中出现的频率。

我试过用length()计算单个单词,结果得到了2,这意味着它不起作用。

有没有什么方法可以计算出一个词在列表中出现的频率,因为我打算将计算出的词存储在一个新列表中,而它的频率则存储在另一个列表中。

提前谢谢


首先要注意的是,texts可能是一个嵌套列表,这也是为什么你得到len(texts)2的原因,因为texts包含2个子列表。

如果要迭代单个单词,需要迭代子列表,然后迭代子列表中的单词。幸运的是,python的列表理解可以嵌套:

1
[word for words in texts for word in words]

至于计数:标准库有一个字典类,目的正是:collections.counter:

1
word_counts = collections.Counter(word for words in texts for word in words)

这将为您提供一个字典,将单个单词映射到它们的出现次数。


一个衬里:

1
2
3
4
5
6
7
8
9
from collections import Counter
from itertools import chain

texts = [["a","b"], ["a","c"]]

words_count = Counter(chain(*texts))
print(words_count)

>> Counter({'a': 2, 'b': 1, 'c': 1})


你可以用计数器来做这个。

1
2
3
4
5
6
7
8
9
10
11
12
texts = [
    ["great","even","for","the","non","runner","this","sound",
     "track","was","brilliant"],
    ["cannot","recommend","as","a","former","comrade","i","did",
     "not","want","to","have","to","do","this"]
]

for text in texts:
    cnt = Counter()
    for word in text:
        cnt[word] += 1
    print(cnt)


你可以用Counter数词:

1
2
3
4
5
6
7
8
9
10
from collections import Counter

texts = [["great","even","for","the","non","runner","this","sound","track","was","brilliant"],
         ["cannot","recommend","as","a","former","comrade","i","did","not","want","to","have","to","do","this"]]

for text in texts:
    print(Counter(text))

# Counter({'great': 1, 'even': 1, 'for': 1, 'the': 1, 'non': 1, 'runner': 1, 'this': 1, 'sound': 1, 'track': 1, 'was': 1, 'brilliant': 1})
# Counter({'to': 2, 'cannot': 1, 'recommend': 1, 'as': 1, 'a': 1, 'former': 1, 'comrade': 1, 'i': 1, 'did': 1, 'not': 1, 'want': 1, 'have': 1, 'do': 1, 'this': 1})

来源:如何计算列表中的唯一值