首页 > 解决方案 > 关于嵌套字典的问题有没有办法将嵌套字典合并到一个字典中

问题描述

我正在学习关于 runestone 的 python 课程,但我遇到了以下问题:

提供了一个包含 pokemon go 玩家数据的字典,其中每个玩家显示每个 pokemon 拥有的糖果数量。如果将所有数据汇总在一起,哪个口袋妖怪的糖果数量最多?将该口袋妖怪分配给变量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

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

谢谢任何建议都非常受欢迎

标签: pythondictionarynested

解决方案


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)

推荐阅读