【发布时间】:2011-07-26 18:18:30
【问题描述】:
我的字典如下:
counts = {('test1', 'alpha'): 2,
('test2', 'beta'): 1,
('test1', 'delta'): 1,
('test2', 'gamma'): 2}
如何返回每个具有最大值的元组的“alpha/beta/gamma/delta”?
即
test1, alpha, 2 #因为 test1 的最高值是 'alpha'
test2, gamma, 2 #因为 test2 的最高值是 'gamma'
这行得通吗?
maxDict={}
for (eachtest,pattern), counter in counts.items():
maxDict[eachtest,pattern] = max(maxDict.get(eachtest,0),counter)
谢谢。
【问题讨论】:
标签: python dictionary max tuples