如何根据单词是另一个列表的成员来计算单词的重复次数

时间:2015-11-10 11:24:34

标签: python list compare

我根据其在另一个列表childList

中的成员资格,尝试计算列表motherList中某些字词的重复计数

例如

childList= ['hello', 'i', 'love', 'cake', 'yay!'

motherList= ['yay!', 'love']

我的问题是双重的:

什么数据结构可以让我记录每个单词的出现?

我如何计算他们的复发次数?

1 个答案:

答案 0 :(得分:4)

您可以使用字典理解和list.count来创建计数字典:

>>> {i:words.count(i) for i in positiveWords}
{'yay!': 1, 'love': 1}