我是一名Python初学者。我写了一个代码,其中参赛者的姓名和他们的分数将存储在字典中。让我把这本词典称为results
。不过,在编写代码时我已将它留空。当程序运行时,键和值将被添加到字典中。如何比较带有未知键的字典的值?
results={}
name=raw_input()
#some lines of code to get the score#
results[name]=score
#code#
name=raw_input()
#some lines of code to get the score#
results[name]=score
程序执行后,让我们说results == {"john":22, "max":20}
我想比较约翰和最大的成绩,并宣布与得分最高的冠军的人。但是在节目开始时我不会知道参赛者的姓名。那么我怎样才能比较分数,并宣布其中一人为胜利者。
names = [];在results.iterkeys()中输入名称:names.append(name) – Ananth
您想在字典中获得最高分数吗? – Arman
请参阅http://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary – Seba