I have a list of lists like :
names = [['cat', 'fish'], ['cat'], ['fish', 'dog', 'cat'],
['cat', 'bird', 'fish'], ['fish', 'bird']]
I want to count number of times that each pair of names mentioned together in the whole list and the output would be like:
{ ['cat', 'fish']: 3, ['cat', 'dog']: 1,['cat','bird']:1
['fish','dog'] : 1, ['fish','bird']:2}
I tried :
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
but it doesn't count co=occurrences in the main list.