Apr-30-2020, 01:05 AM
First, I have a list of dicts with the info of some athletes which looks like this:
{"medal":} can have "gold", "silver", "bronze" or "na" (not applicable)
I've tried doing this:
athletes = [{"name":"Robert", "country":"england", "event": "athletics men's 50000 metres", "medal":"gold"}, {"name":"Jan", "country":"england", "event": "athletics men's 50000 metres", "medal":"silver"}....]I need to create a dict based on the name of Robert's country and the number of medals they (the country in a specific event) have obtained, it should look like this:
{'canada': [0, 0, 1], 'china': [1, 0, 0]...}The first position represents gold medals, second, silver and third bronze.
{"medal":} can have "gold", "silver", "bronze" or "na" (not applicable)
I've tried doing this:
def funct(athletes:list, event:str): import copy list1 = [0, 0, 0] dicc = {} for i in athletes: if i["event"] == event and i["medal"] != 'na': dicc[i["country"]] = copy.deepcopy(list1) for country in dicc.keys(): if country == i["country"] and i["medal"] == "gold": dicc[country][0] +=1 elif country == i["country"] and i["medal"] == "silver": dicc[country][1] +=1 elif country == i["country"] and i["medal"] == "bronze": dicc[country][2] +=1 return dicc print(funct(athletes, "athletics men's 50000 metres"))It works, but I'm just getting the info of the last athlete of that country (using the list from above):
{'england': [0, 1, 0]}When I should be getting (expected result):
{'england': [, 1, 0]}What should I do to fix this?