1
唯一值的我有这样一个字典:计数嵌套字典蟒蛇
dict = {
"a": {"Azerbaijan": 20006.0, "Germany": 20016.571428571428},
"b": {"Chad": 13000.0, "South Africa": 3000000.0},
"c": {"Chad": 200061.0, "South Africa": 3000000.0}
}
,我试图让每一个独特的国家和值的出现的计数的字典。
例如,{"Chad": 2, "South Africa": 2,..}
,{"3000000": 2, "13000": 1,..}
我使用下面这些作品,但不是很聪明,有没有更好的办法做到这一点不长迭代周期的代码,因为实际的字典是巨大的?
seencountries = {}
seenvalues = {}
for key, innerdict in dict.iteritems():
for country, value in innerdict.iteritems():
if value not in seenvalues.keys():
seenvalues[value] = 0
seenvalues[value]+=1
if country not in seencountries.keys():
seencountries[country] = 0
seencountries[country]+=1
print seencountries
print seenvalues
你的国家是*键*?由于Python字典没有排序 - 您如何知道从该国的每个字典中取得哪个关键字? –
如果没有看到。字典只包含独特的价值,如果我理解正确的话? –
它们包含唯一的键 - 这很正确。但是 - 按键的顺序是任意的......或者你计算所有的键,而不仅仅是国家......?我相当肯定的关键* a *这些键都不是国家... –