有关嵌套字典的问题,有没有一种方法可以将嵌套字典合并到一个字典中

时间:2019-09-09 21:25:11

标签: python dictionary nested

我正在关注符石的python课程,并且遇到以下问题:

提供了一个包含pokemon go玩家数据的字典,其中每个玩家都可以看到每个口袋妖怪所拥有的糖果量。如果将所有数据汇总在一起,哪个口袋妖怪的糖果数量最多?将该神奇宝贝分配给变量most_common_pokemon

我以为是要创建一个字典来合并公用键(及其值),或者进行类似

的比较
if x>y
   x=y

所以我可以得到最多糖果的口袋妖怪

pokemon_go_data = {'bentspoon':
                  {'Rattata': 203, 'Pidgey':20, 'Drowzee': 89, 'Squirtle': 35, 'Pikachu': 3, 'Eevee': 34, 'Magikarp': 300, 'Paras': 38},
                  'Laurne':
                  {'Pidgey': 169, 'Rattata': 245, 'Squirtle': 9, 'Caterpie': 38, 'Weedle': 97, 'Pikachu': 6, 'Nidoran': 44, 'Clefairy': 15, 'Zubat': 79, 'Dratini': 4},
                  'picklejarlid':
                  {'Rattata': 32, 'Drowzee': 15, 'Nidoran': 4, 'Bulbasaur': 3, 'Pidgey': 56, 'Weedle': 21, 'Oddish': 18, 'Magmar': 6, 'Spearow': 14},
                  'professoroak':
                  {'Charmander': 11, 'Ponyta': 9, 'Rattata': 107, 'Belsprout': 29, 'Seel': 19, 'Pidgey': 93, 'Shellder': 43, 'Drowzee': 245, 'Tauros': 18, 'Lapras': 18}}

pokemon=[]

for i,k in pokemon_go_data.items():
    b=k.keys()
    b=list(b)
    pokemon.append(b)
print (pokemon)  

poke=[]

for i in pokemon:
    for j in i:
        if j not  in poke:
            poke.append(j)
        else:
            continue
print(poke) 

d={}
n=0
count=[]
total=0
most_common_pokemon=""

for players in pokemon_go_data:
    for pokemon in pokemon_go_data[players]:
            if pokemon==poke[n]:
                 count.append(pokemon_go_data[players][pokemon])
                 counts=sum(count)
                 print (count)
                 print(counts)
                 d[poke[n]]=counts
print (d) 

这样做会打印字典:{'Rattata': 587}

但是如果我添加一个类似n+=1的计数器,则会得到以下内容

{'Rattata': 203, 'Pidgey': 372, 'Drowzee': 387}

如果不是创建字典,例如

if count>total:
            total=count
            most_common_pokemon=poke[n]
n+1=n

我收到超出范围的错误消息 我将计数器放置在任何地方,但是它不起作用...而且当我重置计数时

非常感谢任何建议

4 个答案:

答案 0 :(得分:0)

这应该做到:

pokemon_total = {}

for player, dictionary in pokemon_go_data.items():
    for pokemon, candy_count in dictionary.items():
        if pokemon in pokemon_total.keys():
            pokemon_total[pokemon] += candy_count
        else:
            pokemon_total[pokemon] = candy_count

most_common_pokemon = max(pokemon_total, key=pokemon_total.get)

print(most_common_pokemon)

答案 1 :(得分:0)

out = {}

for k,v in [[k2,p[k1][k2]] for k1 in p for k2 in p[k1]]:
    if k in out.keys():
        out[k] = out[k] + v
    else:
        out[k] = v

print(max(out, key=out.get))

原则上与上述答案基本相同,但实现上略有差异

OR

from itertools import groupby    
out = sorted([[k2,p[k1][k2]] for k1 in p for k2 in p[k1]])
result = {a:sum(c for _, c in b) for a, b in groupby(out, key=lambda x:x[0])}
print(max(result,key=result.get))

OR

out = sum(map(Counter, p.values()), Counter())
print(max(out,key=result.get))

答案 2 :(得分:0)

pokemon_total = {}

for player, dictionary in pokemon_go_data.items():
    for pokemon, candy_count in dictionary.items():
        if pokemon in pokemon_total.keys():
            pokemon_total[pokemon] += candy_count
        else:
            pokemon_total[pokemon] = candy_count

max_occurence = 0
for pokemon in pokemon_total:
    if pokemon_total[pokemon] > max_occurence :
        max_occurence = pokemon_total[pokemon]
        most_common_pokemon = pokemon    
print(most_common_pokemon)

答案 3 :(得分:0)

T 刚刚这样做对我有用并给出了正确的答案“Rattata”

pokemon_go_data = {'bentspoon':
                       {'Rattata': 203, 'Pidgey': 120, 'Drowzee': 89, 'Squirtle': 35, 'Pikachu': 3, 'Eevee': 34,
                        'Magikarp': 300, 'Paras': 38},
                   'Laurne':
                       {'Pidgey': 169, 'Rattata': 245, 'Squirtle': 9, 'Caterpie': 38, 'Weedle': 97, 'Pikachu': 6,
                        'Nidoran': 44, 'Clefairy': 15, 'Zubat': 79, 'Dratini': 4},
                   'picklejarlid':
                       {'Rattata': 32, 'Drowzee': 15, 'Nidoran': 4, 'Bulbasaur': 3, 'Pidgey': 56, 'Weedle': 21,
                        'Oddish': 18, 'Magmar': 6, 'Spearow': 14},
                   'professoroak':
                       {'Charmander': 11, 'Ponyta': 9, 'Rattata': 107, 'Belsprout': 29, 'Seel': 19, 'Pidgey': 93,
                        'Shellder': 43, 'Drowzee': 245, 'Tauros': 18, 'Lapras': 18}}

count_d={}
pokemon_main_lst=pokemon_go_data.keys()
#print(pokemon_main_lst)
for main_keys in pokemon_main_lst:
    pokemon_sub_lst=pokemon_go_data[main_keys].keys()
    #print(sub_lst)
    for pokemon in pokemon_sub_lst:
        if pokemon not in count_d:
            count_d[pokemon]=0
        count_d[pokemon]+=pokemon_go_data[main_keys][pokemon]
#print(count_d)

most_common_pokemon=sorted(count_d,key=lambda k:count_d[k])[-1]
print(most_common_pokemon)