Python 获取字典中最小值对应的key

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3282823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:24:24  来源:igfitidea点击:

Get the key corresponding to the minimum value within a dictionary

pythondictionaryminminimumidioms

提问by tjvr

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

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

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

我正在考虑与min()功能有关的事情......

Given the input:

鉴于输入:

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

It would return 321.

它会返回321

采纳答案by Alex Martelli

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

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

回答by perimosocordiae

Edit:this is an answer to the OP's originalquestion about the minimal key, not the minimal answer.

编辑:这是 OP关于最小键的原始问题的答案,而不是最小答案。



You can get the keys of the dict using the keysfunction, and you're right about using minto find the minimum of that list.

您可以使用该keys函数获取 dict 的键,并且使用该函数min查找该列表中的最小值是正确的。

回答by Mark Rushakoff

Here's an answer that actually gives the solution the OP asked for:

这是一个实际上给出了 OP 要求的解决方案的答案:

>>> d = {320:1, 321:0, 322:3}
>>> d.items()
[(320, 1), (321, 0), (322, 3)]
>>> # find the minimum by comparing the second element of each tuple
>>> min(d.items(), key=lambda x: x[1]) 
(321, 0)

Using d.iteritems()will be more efficient for larger dictionaries, however.

d.iteritems()但是,对于较大的字典,使用会更有效。

回答by user365363

Is this what you are looking for?

这是你想要的?

d = dict()
d[15.0]='fifteen'
d[14.0]='fourteen'
d[14.5]='fourteenandhalf'

print d[min(d.keys())]

Prints 'fourteen'

打印“十四”

回答by abyx

min(d.items(), key=lambda x: x[1])[0]

min(d.items(), key=lambda x: x[1])[0]

回答by eruciform

# python 
d={320:1, 321:0, 322:3}
reduce(lambda x,y: x if d[x]<=d[y] else y, d.iterkeys())
  321

回答by Daniel Stutzbach

>>> d = {320:1, 321:0, 322:3}
>>> min(d, key=lambda k: d[k]) 
321

回答by Tony Veijalainen

If you are not sure that you have not multiple minimum values, I would suggest:

如果您不确定是否没有多个最小值,我建议:

d = {320:1, 321:0, 322:3, 323:0}
print ', '.join(str(key) for min_value in (min(d.values()),) for key in d if d[key]==min_value)

"""Output:
321, 323
"""

回答by PaulMcG

Another approach to addressing the issue of multiple keys with the same min value:

解决具有相同最小值的多个键的问题的另一种方法:

>>> dd = {320:1, 321:0, 322:3, 323:0}
>>>
>>> from itertools import groupby
>>> from operator import itemgetter
>>>
>>> print [v for k,v in groupby(sorted((v,k) for k,v in dd.iteritems()), key=itemgetter(0)).next()[1]]
[321, 323]

回答by Antti Haapala

Use minwith an iterator (for python 3 use itemsinstead of iteritems); instead of lambda use the itemgetterfrom operator, which is faster than lambda.

使用min与迭代器(对于Python 3使用items代替iteritems); 使用itemgetterfrom 运算符代替 lambda ,它比 lambda 更快。

from operator import itemgetter
min_key, _ = min(d.iteritems(), key=itemgetter(1))