skip to Main Content

I am supposed to run a piece of code that finds all the affiliations of a Nobel Prize winner with a function get_affiliation_prizes(dict_laureates). This is an example of the data structure:

 {'id': '2',
   'firstname': 'Hendrik A.',
   'surname': 'Lorentz',
   'born': '1853-07-18',
   'died': '1928-02-04',
   'bornCountry': 'the Netherlands',
   'bornCountryCode': 'NL',
   'bornCity': 'Arnhem',
   'diedCountry': 'the Netherlands',
   'diedCountryCode': 'NL',
   'gender': 'male',
   'prizes': [{'year': '1902',
     'category': 'physics',
     'share': '2',
     'motivation': '"in recognition of the extraordinary service they rendered by their researches into the influence of magnetism upon radiation phenomena"',
     'affiliations': [{'name': 'Leiden University',
       'city': 'Leiden',
       'country': 'the Netherlands'}]}]},
  {'id': '3',
   'firstname': 'Pieter',
   'surname': 'Zeeman',
   'born': '1865-05-25',
   'died': '1943-10-09',
   'bornCountry': 'the Netherlands',
   'bornCountryCode': 'NL',
   'bornCity': 'Zonnemaire',
   'diedCountry': 'the Netherlands',
   'diedCountryCode': 'NL',
   'diedCity': 'Amsterdam',
   'gender': 'male',
   'prizes': [{'year': '1902',
     'category': 'physics',
     'share': '2',
     'motivation': '"in recognition of the extraordinary service they rendered by their researches into the influence of magnetism upon radiation phenomena"',
     'affiliations': [{'name': 'Amsterdam University',
       'city': 'Amsterdam',
       'country': 'the Netherlands'}]}]},

and this is the code that I wrote:

def get_affiliation_prizes(dict_laureates):
    affiliation_prizes = {}
    for laureate in dict_laureates:
        if 'prizes' in laureate and 'affiliations' in laureate:
            for prize in laureate['affiliations']:
                category = prize.get('category')
                year = prize.get('year')
                if category and year:
                    affiliation_prizes[affiliation].append({'category': category, 'year': year})
    return affiliation_prizes
    

with open('../Data/json_data/NobelPrize/laureate.json', 'r', encoding='utf-8') as file:
    dict_laureates = json.load(file)

affiliation_prizes = get_affiliation_prizes(dict_laureates)
print(affiliation_prizes)

However, it keeps returning an empty dictionary

I tried messing around a bit with the if statements to see if removing or adding some of the conditionals helped, but it either broke the code or it still returned an empty dictionary.

What I am expecting to happen (or rather hope to happen) is the code to return a nested dictionary in the following format:

{
    "A.F. Ioffe Physico-Technical Institute": [
        {"category": "physics", "year": "2000"}
    ],
    "Aarhus University": [
        {"category": "chemistry", "year": "1997"},
        {"category": "economics","year": "2010"}
    ]
}

The way Aarhus University is supposed to have two dictionaries is (I believe) because they are affiliates in two Nobel prizes:

{'id': '857',
   'firstname': 'Dale T.',
   'surname': 'Mortensen',
   'born': '1939-02-02',
   'died': '2014-01-09',
   'bornCountry': 'USA',
   'bornCountryCode': 'US',
   'bornCity': 'Enterprise, OR',
   'diedCountry': 'USA',
   'diedCountryCode': 'US',
   'diedCity': 'Wilmette, IL',
   'gender': 'male',
   'prizes': [{'year': '2010',
     'category': 'economics',
     'share': '3',
     'motivation': '"for their analysis of markets with search frictions"',
     'affiliations': [{'name': 'Northwestern University',
       'city': 'Evanston, IL',
       'country': 'USA'},
      {'name': 'Aarhus University',
       'city': 'Aarhus',
       'country': 'Denmark'}]}]},

and

      {'id': '289',
       'firstname': 'Jens C.',
       'surname': 'Skou',
       'born': '1918-10-08',
       'died': '2018-05-28',
       'bornCountry': 'Denmark',
       'bornCountryCode': 'DK',
       'bornCity': 'Lemvig',
       'diedCountry': 'Denmark',
       'diedCountryCode': 'DK',
       'diedCity': 'Aarhus',
       'gender': 'male',
       'prizes': [{'year': '1997',
         'category': 'chemistry',
         'share': '2',
         'motivation': '"for the first discovery of an ion-transporting enzyme, Na+, K+ -ATPase"',
         'affiliations': [{'name': 'Aarhus University',
           'city': 'Aarhus',
           'country': 'Denmark'}]}]},

2

Answers


  1. Your code encountered an error, so you were never able to delve deep enough into it to find other errors.

    Your function should have further delved into the 'affiliations' key within each prize:

    def get_affiliation_prizes(dict_laureates):
        affiliation_prizes = {}
        for laureate in dict_laureates:
            if 'prizes' in laureate:
                for prize in laureate['prizes']:
                    if 'affiliations' in prize:
                        category = prize.get('category')
                        year = prize.get('year')
                        if category and year:
                            for affiliation in prize['affiliations']:
                                affiliation_name = affiliation['name']
                                prizes = affiliation_prizes.get(affiliation_name, None)
                                if not prizes:
                                    affiliation_prizes[affiliation_name] = []
                                affiliation_prizes[affiliation_name].append({'category': category, 'year': year})
        return affiliation_prizes
    
    Login or Signup to reply.
  2. Check your JSON in a validator like https://jsonlint.com/.

    Note also that nested structures are not easy to maintain as the code becomes larger.
    To avoid too much nesting, quit the execution of the current iteration of your for loops by using continue.
    As a rule of thumb, it is best to test early if execution must continue or not in loops and in functions.

    You should also check that all fields that you want to read are present.
    If not, you should decide what to do, for example return an empty string or nothing at all.

    Note also that you called the function get_affiliation_prizes but in fact it should be get_laureate_affiliations.

    import json
    from pprint import pprint
    
    
    def get_laureate_affiliations(dict_laureates):
        laureate_affiliations = []
        for laureate in dict_laureates:
            if "prizes" not in laureate:
                continue
            for prize in laureate["prizes"]:
                if "affiliations" not in prize:
                    continue
            for prize in laureate["prizes"]:
                for affiliation in prize["affiliations"]:
                    affiliation_name = affiliation["name"] if "name" in affiliation else ""
                    prize_category = prize["category"] if "category" in prize else ""
                    prize_year = prize["year"] if "year" in prize else ""
                    laureate_affiliation = {
                        affiliation_name: {"category": prize_category, "year": prize_year}
                    }
                    laureate_affiliations.append(laureate_affiliation)
        return laureate_affiliations
    
    
    def main():
        with open("laureate.json", "r", encoding="utf-8") as file:
            dict_laureates = json.load(file)
        affiliation_prizes = get_laureate_affiliations(dict_laureates)
        pprint(affiliation_prizes)
    
    
    if __name__ == "__main__":
        main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search