我有一個字典如下:的Python:通過返回一個字典的最大值其tupled密鑰
counts = {('test1', 'alpha'): 2,
('test2', 'beta'): 1,
('test1', 'delta'): 1,
('test2', 'gamma'): 2}
我怎樣才能返回其具有每個元組中的「α/β/γ/δ」最大值?
即
爲test1,α,2 #because test1的具有 '阿爾法' 作爲最高價值
test2的,伽瑪,2個#because test2的具有 'γ' 作爲最高價值
將這工作?
maxDict={}
for (eachtest,pattern), counter in counts.items():
maxDict[eachtest,pattern] = max(maxDict.get(eachtest,0),counter)
謝謝。