Skip to content Skip to sidebar Skip to footer

Trying To Fix My Function

I'm working on a function where I have to return a tuple where the first parameter is a str of the highest number and the second parameter is a list of int. Here's the example and

Solution 1:

You have to map the position of the maxvalue to the correct party:

parties = ['NDP', 'Green', 'Liberal', 'CPC']
winning_party = parties[total.index(max(total))]

Solution 2:

Try using Counter to count how many votes each element got. For example:

from collections import Counter
...
vote_count = Counter(votes_list)
int_list = vote_count.values() # value is [1, 3, 1]
winners = vote_count.most_common() # value is [('G', 3), ('C', 1), ('N', 1)]

As you can see, Counter has an interface to both give you the vote count for each element, and give you all the elements in descending order of votes.

Post a Comment for "Trying To Fix My Function"