Python中获取字典中最值对应的键
- 利用
min(dict, key=dict.get)
>>> d = {1:1, 2:0, 3:2}
{1: 1, 2: 0, 3: 2}
>>> min(d, key=d.get)
2
- 利用lambda函数
>>> min(d.items(), key=lambda x: x[1])
(2, 0)
>>> min(d, key=lambda x: d[x])
2
min(dict, key=dict.get)
>>> d = {1:1, 2:0, 3:2}
{1: 1, 2: 0, 3: 2}
>>> min(d, key=d.get)
2
>>> min(d.items(), key=lambda x: x[1])
(2, 0)
>>> min(d, key=lambda x: d[x])
2