我需要從字典中獲取最大值的幫助。我有{num:[state,value] ...}的字典,並且需要獲取與最高值關聯的所有內容。搜索字典獲取最大值,然後獲取關聯密鑰
#find max value in dictionary
td = {0: ['b',3], 1: ['b',6], 4: ['b',2], 3: ['b',5] }
#In this example td dict, I want to grab the key and state associated with the highest value, 6. I want to grab "1: ['b',6]"
print td
print td.keys()
print td.values()
maxval = max([v for k,[s,v] in td.iteritems()])
print maxval #correctly prints 6
你確定只有一個(k,s,v)三元組達到最大值v嗎? – DSM 2012-03-13 23:45:48