TransWikia.com

Get average value of keys in a dictionary that are in a nested dictionary

Stack Overflow Asked by user12344 on October 11, 2020

Say I want to get the average values for all "Jazz"-keys, however, I want to do it for each separate key within the nested dictionary.

def average_ratings(aDict) :
    for key in aDict :
        if "Jazz" in aDict:
            mySum = 0
            numNums = 0
            while value in aDict['Jazz'] :
                mySum = mySum + value
                numNums = numNums + 1
            jazzValue = mySum / numNums
            return jazzValue
        if "Rock" in aDict :
            summ = 0
            num = 0
            while value in aDict["Rock"] :
                summ = summ + 1
                num = num + 1
            rockValue = summ / num
            return rockValue

This is the dictionary I am passing through

{'Justin Trudeau': {'Hits': 4, 'Jazz': 3, 'Rock': 7, 'Reggae': 7, 'Dance': 7, 'Blues': 6, 'Rap': 4}, 'Bob Jones': {'EDM': 4, 'Dance': 3, 'Hits': 1, 'Rock': 3, 'Opera': 4, 'Funk': 3, 'Soul': 6, 'Metal': 8, 'Classical': 9}, 'Sam Frizzel': {'Rap': 7, 'Hits': 6, 'Soul': 2, 'EDM': 5, 'Pop': 3, 'Reggae': 3, 'Hip Hop': 5}, 'Captain Nemo': {'Dance': 4, 'Rock': 2, 'Classical': 10, 'World': 8, 'Jazz': 9, 'Hip Hop': 6, 'Soul': 4, 'Metal': 2}}

One Answer

In [33]: a = {'Justin Trudeau': {'Hits': 4, 'Jazz': 3, 'Rock': 7, 'Reggae': 7, 'Dance': 7, 'Blues': 6, 'Rap': 4}, 'Bob
    ...:  Jones': {'EDM': 4, 'Dance': 3, 'Hits': 1, 'Rock': 3, 'Opera': 4, 'Funk': 3, 'Soul': 6, 'Metal': 8, 'Classica
    ...: l': 9},'Sam Frizzel': {'Rap': 7, 'Hits': 6, 'Soul': 2, 'EDM': 5, 'Pop': 3, 'Reggae': 3, 'Hip Hop': 5}}
    ...:

In [34]: jazz = [a[i].get('Jazz',0) for i in a if 'Jazz' in a[i]]

In [35]: print(sum(jazz)/len(jazz))
3.0

Update:

from collections import defaultdict

a = {'Justin Trudeau': {'Hits': 4, 'Jazz': 3, 'Rock': 7, 'Reggae': 7, 'Dance': 7, 'Blues': 6, 'Rap': 4}, 'Bob Jones': {'EDM': 4, 'Dance': 3, 'Hits': 1, 'Rock': 3, 'Opera': 4, 'Funk': 3, 'Soul': 6, 'Metal': 8, 'Classical': 9}, 'Sam Frizzel': {'Rap': 7, 'Hits': 6, 'Soul': 2, 'EDM': 5, 'Pop': 3, 'Reggae': 3, 'Hip Hop': 5}, 'Captain Nemo': {'Dance': 4, 'Rock': 2, 'Classical': 10, 'World': 8, 'Jazz': 9, 'Hip Hop': 6, 'Soul': 4, 'Metal': 2}}

avgs = defaultdict(list)

for k,v in a.items():
    for i in v:
        avgs[i].append(v[i])
    
for k,v in avgs.items():
    print(f"Average of {k} is {sum(v)/len(v)}")

Output:

Average of Hits is 3.6666666666666665
Average of Jazz is 6.0
Average of Rock is 4.0
Average of Reggae is 5.0
Average of Dance is 4.666666666666667
Average of Blues is 6.0
Average of Rap is 5.5
Average of EDM is 4.5
Average of Opera is 4.0
Average of Funk is 3.0
Average of Soul is 4.0
Average of Metal is 5.0
Average of Classical is 9.5
Average of Pop is 3.0
Average of Hip Hop is 5.5
Average of World is 8.0

Correct answer by bigbounty on October 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP