Search code examples
pythondictionarynested

question regarding a nested dictionary is there a way to merge a nested dictionary in one dictionary


I'm following a python course on runestone and i'm stuck with the following question:

Provided is a dictionary that contains pokemon go player data, where each player reveals the amount of candy each of their pokemon have. If you pooled all the data together, which pokemon has the highest number of candy? Assign that pokemon to the variable most_common_pokemon.

what i thought is to created a dictionary that merge the common keys (and their value or to make a comparison something like

if x>y
   x=y

so I can get the pokemon with the highest number of candies

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) 

by doing so it prints a dictionary: {'Rattata': 587}

but if i add a counter like n+=1 i got the following

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

if instead of creating a dictionary something like

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

i got a out of range error message i placed the counter everywhere but it doesn't work...and also when i reset count

thanks any suggestion is more than welcome


Solution

  • This should do it:

    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)