我正在處理一個函數,我必須返回一個元組,其中第一個參數是最高數字的str,第二個參數是int列表。這裏的例子和我寫爲函數:試圖修復我的功能
投票([ 'G', 'G', 'N', 'G', 'C']) ( 'G' ,[1,3,0,1]) 「」」
我正在處理一個函數,我必須返回一個元組,其中第一個參數是最高數字的str,第二個參數是int列表。這裏的例子和我寫爲函數:試圖修復我的功能
投票([ 'G', 'G', 'N', 'G', 'C']) ( 'G' ,[1,3,0,1]) 「」」
你必須映射MAXVALUE的正確方的位置:
parties = ['NDP', 'Green', 'Liberal', 'CPC']
winning_party = parties[total.index(max(total))]
非常感謝!這樣可行。 –
嘗試使用Counter
來計算每個元素獲得的投票數。例如:
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)]
正如你所看到的,Counter
有一個接口,既給你的每個元素計票,並給你按降序票的順序排列的所有元素。
相關http://stackoverflow.com/questions/15733220/python-list-voteg-g-n-g-c? – YXD