Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python counter nested dictionary

import collections
a = collections.defaultdict(collections.Counter)
inf = [('fruit','apple'),('car','truck'),('fruit','banana'),('fruit','banana')]
for category,item in inf:
    a[category][item] = a[category][item] + 1   
#print(a)
#{'fruit': Counter({'banana': 2, 'apple': 1}), 'car': Counter({'truck': 1})})
 
PREVIOUS NEXT
Tagged: #python #counter #nested #dictionary
ADD COMMENT
Topic
Name
5+6 =