Trying to fix my function

后端 未结 2 1854
有刺的猬
有刺的猬 2021-01-28 20:15

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

相关标签:
2条回答
  • 2021-01-28 20:23

    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))]
    
    0 讨论(0)
  • 2021-01-28 20:34

    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.

    0 讨论(0)
提交回复
热议问题