Answers for "python counter nested dictionary"

1

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})})
Posted by: Guest on June-21-2021

Code answers related to "python counter nested dictionary"

Python Answers by Framework

Browse Popular Code Answers by Language