How to properly eliminate elements in dictionary until one string remains

前端 未结 2 1208
梦谈多话
梦谈多话 2021-01-26 09:28

I really need help on this

def get_winner (dict_winner):
    new_dict = {} 
    for winner in dict_winner:
        first_letter = winner[0]
        value = dict_         


        
2条回答
  •  执念已碎
    2021-01-26 10:04

    My solution - in one step:

    def get_winner(candidates):
        winners = dict.fromkeys(map(lambda f: f[0] for f in candidates.keys()))
        for cand, votes in candidates.iteritems(): 
            winners[cand[0]]+=votes
        return [winner for winner, vote in winners.iteritems() if vote ==max(winners.values())]
    

    It is not fancy, but it is simple :-)

提交回复
热议问题