Fraa Smith
Fraa Smith

Reputation: 29

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 what I wrote for the function:

voting(['G', 'G', 'N', 'G', 'C']) ('G', [1, 3, 0, 1]) """

Upvotes: 0

Views: 103

Answers (2)

ehudt
ehudt

Reputation: 810

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.

Upvotes: 0

Benjamin
Benjamin

Reputation: 609

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))]

Upvotes: 1

Related Questions