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
Обновление:
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)}")
Вывод:
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