-1
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]))
  • Based on your comment to Abdul's answer, I encourage you to read this: https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions – BrokenBenchmark Nov 11 '22 at 06:38

2 Answers2

1

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}
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
1

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}
Mehdi
  • 999
  • 13
  • 11