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'}]}]},
>Solution :
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