Python-在字典中找到列表的总和,然后找到值最低的列表

时间:2020-08-18 02:37:05

标签: python

我正在为纸牌游戏创建一个简单的计分员。得分最低的人获胜。我在字典中保留球员和得分:

players = {'bob': [3,6,4,3,7,3,6], 'joe': [3,7,5,9,9,7,8]}

我可以通过以下方式总结每个玩家的积分:

for player, score in players.items():
        print(f"\n{player.title()}'s final score is {sum(score)}")

现在,我想宣布获胜者(得分最低的人),但我无法使其工作。这是我尝试过的:

for points in players.values():
    score = sum(points)
winner = min(score)
print(f"The winner is {winner}!")

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

尝试一下

winner=min(players, key=lambda p:sum(players[p]))

答案 1 :(得分:0)

print('The winner is {}'.format(min(players, key=lambda k: sum(players[k]))))

答案 2 :(得分:0)

首先声明一个大数字,一个空的获胜者变量

创建一个dictionary语句,该语句打印所有point = 1000000; winner = '' for name,points in players.items(): temp = sum(points) #sum of all the num in the list if point > temp: # if point is > than the sum of the list replace the value of points point = temp winner = name print(f"The winner is {winner}!") 值的名称和点

xterm control sequences
相关问题