Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need to create a for loop that generates nested dictionaries

Tags:

python

I need to create a for loop that generates a new nested dictionary each time a key that does not already exist is detected. I am getting the info for the outer dictionary's from a previous function.

  • It will need to create a dictionary with the available sports as its keys and a dictionary as its value.
  • Within the inner dictionary, the athlete name will be used as its key and the number of medals (integer) will be its value. Key = Sport, Value = {: } CSE 231 Spring 2019
  • The function will loop through the dictionary from get_country_stats() to find the athlete, sport and medal. Note that when you want to add an athlete for a new sport you need to first create an empty dictionary for that sport before you can add an athlete to it.
  • The type of medal (gold, silver, bronze) is not relevant for our new dictionary, they will all be treated as 1 medal.

I've initiated two empty dictionaries, the outer and inner ones. Then created an outer for loop that goes through all the key value pairs and returns a list

def display_best_athletes_per_sport(Athlete, Country, Sports):
    medals = 0
    outer_dict = {}
    inner_dict = {}
    for key, value in Country.items(): 
        for item in value:
            athlete = item[0]
            medals = item[5]
            sport = item[3]
            inner_dict = {athlete:medals}
            outer_dict = {sport:inner_dict}
        if sport not in outer_dict:
            new_dict[sport] = value[i]
            if medals in value:
                medals += 1
            else:
                medals = 1

I want to be able to generate a new outer dictionary if the desired key(sport) isn't found, tthen update the inner dictionary's value each time a medal is found for a specific athlete.

this is the output from the Country function that is being used in the function im trying to make now:

{'FIN': [
    ('Juhamatti Tapio Aaltonen', 'Finland', 2014, 'ice hockey', "ice hockey men's ice hockey", 'bronze'),
    ('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's individual all-around", 'bronze'),
    ('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's team all-around", 'gold'),
    ('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's horse vault", 'gold'),
    ('Paavo Johannes Aaltonen', 'Finland', 1948, 'gymnastics', "gymnastics men's pommelled horse", 'gold'),
    ('Paavo Johannes Aaltonen', 'Finland', 1952, 'gymnastics', "gymnastics men's team all-around", 'bronze')],
'NOR': [
    ('Kjetil Andr Aamodt', 'Norway', 1992, 'alpine skiing', "alpine skiing men's super g", 'gold'),
    ('Kjetil Andr Aamodt', 'Norway', 1992, 'alpine skiing', "alpine skiing men's giant slalom", 'bronze'),
    ('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's downhill", 'silver'),
    ('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's super g", 'bronze'),
    ('Kjetil Andr Aamodt', 'Norway', 1994, 'alpine skiing', "alpine skiing men's combined", 'silver'),
    ('Kjetil Andr Aamodt', 'Norway', 2002, 'alpine skiing', "alpine skiing men's super g", 'gold'),
    ('Kjetil Andr Aamodt', 'Norway', 2002, 'alpine skiing', "alpine skiing men's combined", 'gold'),
    ('Kjetil Andr Aamodt', 'Norway', 2006, 'alpine skiing', "alpine skiing men's super g", 'gold'),
    ('Ann Kristin Aarnes', 'Norway', 1996, 'football', "football women's football", 'bronze')],
'NED': [('Pepijn Aardewijn', 'Netherlands', 1996, 'rowing', "rowing men's lightweight double sculls", 'silver')]}
like image 813
alexia231 Avatar asked Nov 26 '25 19:11

alexia231


1 Answers

Well, the cat's out of the bag now. So here's my take on this. I think it's good to separate checking for an entry to appear in a dict from adding something to that entry. So when you add the entry, it always "has nothing in it yet". This lets you treat "adding the next item to the entry" in the same way regardless of if the entry exists or not. Given that, here's the basic idea of what you want to do, as I see it:

def display_best_athletes_per_sport(Athlete, Country, Sports):
    outer_dict = {}
    for key, value in Country.items():
        for item in value:
            athlete = item[0]
            medals = item[5]
            sport = item[3]
            if sport not in outer_dict:
                outer_dict[sport] = {}
            if athlete not in outer_dict[sport]:
                outer_dict[sport][athlete] = 0
            outer_dict[sport][athlete] += 1
    pprint(outer_dict)

and here's the result:

{'alpine skiing': {'Kjetil Andr Aamodt': 8},
 'football': {'Ann Kristin Aarnes': 1},
 'gymnastics': {'Paavo Johannes Aaltonen': 5},
 'ice hockey': {'Juhamatti Tapio Aaltonen': 1},
 'rowing': {'Pepijn Aardewijn': 1}}

This is the same as the answer @gmds provides, so both are valid ways to attack the problem, and do so quite similarly.

like image 197
CryptoFool Avatar answered Dec 02 '25 22:12

CryptoFool