【发布时间】:2012-12-15 00:35:51
【问题描述】:
我正在尝试获取其值是所有 dict 值中最大值的 dict 键。
我找到了两种方式,都不够优雅。
d= {'a':2,'b':5,'c':3}
# 1st way
print [k for k in d.keys() if d[k] == max(d.values())][0]
# 2nd way
print Counter(d).most_common(1)[0][0]
有更好的方法吗?
【问题讨论】:
标签: python dictionary counter