提问人:Onik Rahman 提问时间:4/22/2022 更新时间:4/22/2022 访问量:553
计算另一个列表中一个单词列表的频率
Count the frequency of a list of words within another list
问:
我有一个列表列表,我想在句子中看到频率:
words = [plates, will]
sentence = [the, plates, will, still, shift, and, the, clouds, will, still, spew,]
我想计算一组单词在列表中被提及的次数。
所以从列表中 [plates,will] 在句子中只提到了 1 次words
我有一个完整的列,我想迭代。
理想的输出是:
句 | 词 | 频率 |
---|---|---|
〔〕〔�� | [盘子,将] | 1 |
〔〕〔�� | [仍然,喷出] | 1 |
我试过这个:
for word in word:
if word in sentence:
counts[word] += 1
else:
counts[word] = 1
也
[[word.count() for word in b if word in row] for row in b]
对正确的输出有什么帮助吗?
答:
0赞
terrafox
4/22/2022
#1
这不是内联的,但它完成了我理解您要求的工作。
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)
0赞
Phantoms
4/22/2022
#2
我认为 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])
评论
for word in word:
毫无意义。looks_like = ["T","h","i","s"]
like = ['T','h','i','s']