Python函数,该函数将从字典中计算值中的项总数,并返回另一个包含项计数的字典


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

我希望Python函数输出为

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

,然后在这个数据上建立一个直方图

res = dict()
for key in customer_data.keys():

res[key] = len(set([sub[key] for sub in customer_data]))

可以使用collection模块中的Counter

>>> 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})

或者你也可以这样做,

>>> 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}

我没有得到直方图部分,但计数部分是某种重复的问题(这里)

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}

相关内容

  • 没有找到相关文章

最新更新