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

Question:

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

Asked By: cenedra

||

Answers:

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)
Answered By: Clade
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))

basically same as above answer in principle but slightly diff implementation

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))
Answered By: Derek Eden
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)
Answered By: 30Coder

T did this just now worked for me and gives the correct answer ‘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)
Answered By: Engineering Student

This may help

d={}
for name,val in pokemon_go_data.items():
    for key in val:
        if key not in d:
            d[key]=0
        d[key]+=pokemon_go_data[name][key]
most_common_pokemon=sorted(d.keys(), key= lambda k:d[k])[-1]
Answered By: Syed Abdur Rafay
pcandy={}
for v in pokemon_go_data.values():
    for j in v:
        pcandy[j]=pcandy.get(j,0)+v[j]

s_sorted=sorted(pcandy, key= lambda x: pcandy[x], reverse=True)
most_common_pokemon=s_sorted[0]
print(most_common_pokemon)
Answered By: M.K.A.
d={}
for pokemons in pokemon_go_data.values():
    for name,value in pokemons.items():
        d[name]=d.get(name,0)+value
most_common_pokemon=sorted(d.keys(), key= lambda k:d[k])[-1]
Answered By: Mohammad Gharib
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.