我根据其在另一个列表childList
motherList
中某些字词的重复计数
childList= ['hello', 'i', 'love', 'cake', 'yay!'
motherList= ['yay!', 'love']
我的问题是双重的:
什么数据结构可以让我记录每个单词的出现?
我如何计算他们的复发次数?
答案 0 :(得分:4)
您可以使用字典理解和list.count
来创建计数字典:
>>> {i:words.count(i) for i in positiveWords}
{'yay!': 1, 'love': 1}