计算一个键的一部分出现在字典python中的次数 [英] Count how many times a part of a key appears in a dictionary python
本文介绍了计算一个键的一部分出现在字典python中的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下字典,我想计算键出现的次数,字典很大。
I have the following dictionary and i want to count how many times keys appear, dictionary is very big.
a = { (1,2):3, (1,3):5, (2,1):6 }
我想要这个结果
1: 3 times
2: 2 times
3: 1 time
推荐答案
>>> from collections import Counter
>>> a = { (1,2):3, (1,3):5, (2,1):6 }
>>>
>>> Counter(j for k in a for j in k)
Counter({1: 3, 2: 2, 3: 1})
这篇关于计算一个键的一部分出现在字典python中的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文