我是一名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