小编Fri*_*loc的帖子

如何在字典中创建值的外观直方图?

使用python 2.6:

我有一个字典,其中每个键包含一个值列表.

我想查看字典中的所有值,并计算每个键出现的次数.

我一直在看itervalues()或

for value in dictionary.values():
Run Code Online (Sandbox Code Playgroud)

一个开始,还有.count()函数,但我需要返回一个直方图.

例如:

print dictionary
Run Code Online (Sandbox Code Playgroud)

会回来的

{'test' : ['spam', 'eggs', 'cheese', 'spam'], 'test2' : ['spam', 'cheese', 'goats']}
Run Code Online (Sandbox Code Playgroud)

我想告诉我一些事情:

{'spam' : 3, 'eggs' : 1, 'cheese': 2, 'goats' : 1}
Run Code Online (Sandbox Code Playgroud)

python dictionary count histogram

1
推荐指数
1
解决办法
919
查看次数

标签 统计

count ×1

dictionary ×1

histogram ×1

python ×1