相关疑难解决方法(0)

按最大值获取dict键

我试图得到dict键,它的值在所有dict的值中都是max.我找到了两种方法,都不够优雅.

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]
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?

python counter dictionary

24
推荐指数
1
解决办法
5万
查看次数

标签 统计

counter ×1

dictionary ×1

python ×1