简体   繁体   English

计算另一个列表中单词列表的频率

[英]Count the frequency of a list of words within another list

I have a list of lists which I want to see the frequency in a sentence:我有一个列表列表,我想在一个句子中查看频率:

words = [plates, will]
sentence = [the, plates, will, still, shift, and, the, clouds, will, still, spew,]

I want to count how many times a set of word has been mentioned in a list.我想计算一组单词在列表中被提及的次数。 So from the list words [plates,will] is mentioned just 1 time in the sentence所以从列表中words [plates,will] 在句子中只提到了 1 次

I have a whole column which I want to iterate.我有一整列我想重复。

Desirable output is:理想的 output 是:

sentence句子 word单词 frequency频率
[the, plates, will, still, shift, and, the, clouds, will, still, spew,] [the, plates, will, still, shift, and, the, clouds, will, still, spew,] [plates,will] [盘子,意志] 1 1个
[the, plates, will, still, shift, and, the, clouds, will, still, spew,] [the, plates, will, still, shift, and, the, clouds, will, still, spew,] [still, spew] [仍然,喷] 1 1个

I have tried this:我试过这个:

for word in word:
    if word in sentence:
        counts[word] += 1
    else:
        counts[word] = 1

also

[[word.count() for word in b if word in row] for row in b]

Any help for the right output?对output有帮助吗?

This is not inline but it does the job I understood you asked for.这不是内联的,但它可以完成我理解你要求的工作。

words = ["plates", "will"]
sentence = ["the", "plates", "will", "still", "shift", "and"]

count = 0
# Go through each word in the sentence
for si in range(len(sentence) - len(words)):
    match = True
    # Compare if the following words match
    for wi, word in enumerate(words):
        # Break if one word is wrong
        if sentence[si + wi] != word:
            match = False
            break
    if match:
        count +=1
print(count)

I think that soultion with Counter is simplier.我认为 Counter 的灵魂更简单。

from collections import Counter

words = ['plates', 'will']
sentence = ['the', 'plates', 'will', 'still', 'shift', 'and', 'the', 'clouds', 'will', 'still', 'spew',]

word_counts = Counter(sentence)

for word in words:
    print(word, word_counts[word])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM