Home > front end >  Python function which will count the total number of items in values from a dictionary and return an
Python function which will count the total number of items in values from a dictionary and return an

Time:11-11

data = {'customer1': ['milk', 'bread'],
 'customer2': ['butter'],
 'customer3': ['beer', 'diapers'],
 'customer4': ['milk', 'bread', 'butter'],
 'customer5': ['bread']}

I want the Python function output to be

{'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1} 

and then also build a histogram on this data

res = dict()
for key in customer_data.keys():
  
    res[key] = len(set([sub[key] for sub in customer_data]))

CodePudding user response:

You can use Counter class from collections module.

>>> data = {
...     "customer1": ["milk", "bread"],
...     "customer2": ["butter"],
...     "customer3": ["beer", "diapers"],
...     "customer4": ["milk", "bread", "butter"],
...     "customer5": ["bread"],
... }
>>> 
>>> from collections import Counter
>>>
>>> print(Counter([val for key, value in data.items() for val in value]))
Counter({'bread': 3, 'milk': 2, 'butter': 2, 'beer': 1, 'diapers': 1})

Alternativey you can also do,

>>> data = {
...     "customer1": ["milk", "bread"],
...     "customer2": ["butter"],
...     "customer3": ["beer", "diapers"],
...     "customer4": ["milk", "bread", "butter"],
...     "customer5": ["bread"],
... }
>>> 
>>> 
>>> 
>>> result = {}
>>> 
>>> for _, values in data.items():
...     for value in values:
...         result[value] = result.setdefault(value, 0)   1
... 
>>> print(result)
{'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1}

CodePudding user response:

I didn't get the histogram part, but the counting part was some sort of a duplicated question (here)

from collections import Counter

count=Counter()
for val in data.values():
    count  = Counter(val)

count = dict(count)

output: {'milk': 2, 'bread': 3, 'butter': 2, 'beer': 1, 'diapers': 1}
  • Related