Python Forum
Python Adding +1 to a list item cointained in a dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Adding +1 to a list item cointained in a dict
#1
First, I have a list of dicts with the info of some athletes which looks like 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?
Reply
#2
Indenting got messed up in your post. I think. As posted the function doesn't even return anything.

From what I can glean from the code I see why you only get the last athlete. Each time you find an athlete that medals in an event you reset the medal count for his country. You probably want to do something more like this:
medals = empty dictionary
for each entry in athletes:
    if entry.event == event
        if entry.country not in medals
             add country to medals
        if entry.medal is gold
             etc...
     return medals
Why do you have an import inside your function? That is an odd place to have an import and you don't need the deep copy for a list of ints.

And don't type ugly code like this:
dicc[i["country"]] = copy.deepcopy(list1)
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
Ugh! No matter how elegant the algorithm it is hidden by hideous punctuation. Code that looks good is easier to read and understand. Use meaningful names and local variables
medals = {}
for athlete in athletes: 
    if athlete['event'] == event:
        country = athlete['country']
        medal = athlete['medal']
        if not country in medals:  # Add country to medal list
            medals[country] = [0, 0, 0]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a dict code in python ariellea88 4 915 Oct-31-2023, 08:45 AM
Last Post: buran
  help with adding duplicates elements together in a list 2ECC3O 5 1,972 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  change item in column based on the item before[solved] amdi40 4 1,713 Jan-11-2022, 04:50 PM
Last Post: amdi40
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,841 May-14-2021, 07:14 AM
Last Post: perfringo
  List index out of range when turning CSV into dict ranbarr 15 6,280 May-12-2021, 10:38 AM
Last Post: ranbarr
  Dict from list - HELP! PLEASE! cherry_cherry 16 5,392 Apr-09-2020, 04:01 AM
Last Post: cherry_cherry
  adding parts of a list Eric7Giants 4 2,676 Nov-17-2019, 05:53 PM
Last Post: buran
  Adding values to list and pickling mefiak 2 2,786 May-31-2018, 08:57 AM
Last Post: mefiak
  adding a number to the list atux_null 4 3,796 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  need help removing an item from a list jhenry 4 4,141 Oct-13-2017, 08:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020