获取字典中最小值对应的key [英] Get the key corresponding to the minimum value within a dictionary

查看:32
本文介绍了获取字典中最小值对应的key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 Python 字典,如何获取包含最小值的条目的键?

If I have a Python dictionary, how do I get the key to the entry which contains the minimum value?

我正在考虑与 min() 函数有关的事情...

I was thinking about something to do with the min() function...

给定输入:

{320:1, 321:0, 322:3}

它会返回321.

推荐答案

Best: min(d, key=d.get) -- 没有理由插入一个无用的 lambda代码> 间接层或提取项目或键!

Best: min(d, key=d.get) -- no reason to interpose a useless lambda indirection layer or extract items or keys!

>>> d = {320: 1, 321: 0, 322: 3}
>>> min(d, key=d.get)
321

这篇关于获取字典中最小值对应的key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆