【发布时间】:2017-02-16 11:12:53
【问题描述】:
我有一个列表,例如:
names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],
['cat', 'bird', 'fish'], ['fish', 'bird']]
我想计算整个列表中每对名称一起提到的次数,输出如下:
{ ['cat', 'fish']: 3, ['cat', 'dog']: 1,['cat','bird']:1
['fish','dog'] : 1, ['fish','bird']:2}
我试过了:
from collections import Counter
from collections import defaultdict
co_occurences = defaultdict(Counter)
for tags in names:
for key in tags:
co_occurences[key].update(tags)
print co_occurences
但它不计算主列表中的 co=occurrences。
【问题讨论】:
标签: python list dictionary count find-occurrences