计算字典中每个键的值数量 [英] Count number of values in dictionary for each key
本文介绍了计算字典中每个键的值数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有如下的字典数据:
dict = {
'a' : ('12','3','12'),
'b' : ('32','121'),
'c' : ('232','9','11'),
'd' : ('32'),
'e' : ('243','232')
}
是否可以计算每个键的值数量?
如果我使用
Is that possible to count number of values for each keys?
if i use
sum(len(v) for v in dict[j].itervalues())
它返回所有值的计数...在上述情况下,它返回11
但我需要特定的键...例如,键'c'给出3(数字
那么有什么方法可以做...
非常感谢!
it returns counts of all values ...in above case it return 11
but i need for specific key... for example for key 'c' gives 3 (number of values it has).
so is there any a method to do it...
thanks alot!
推荐答案
new_dict = {k: len(v) for k,v in dict.items()}
这篇关于计算字典中每个键的值数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文