用键输出字典中的最大值 [英] print highest value in dict with key
本文介绍了用键输出字典中的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的字典就像
{'A':4,'B':10,'C':0,'D':87}
我想用键找到最大值,用键找到最小值.
I want to find max value with its key and min value with its key.
输出将类似于,
max:87,键是D
max : 87 , key is D
min:0,密钥为C
min : 0 , key is C
我知道如何从dict中获取最小值和最大值.有什么方法可以在一条语句中获得价值和关键?
I know how to get min and max values from dict. Is there is any way to get value and key in one statement?
max([i for i in dic.values()])
min([i for i in dic.values()])
推荐答案
您可以使用use max
和
You could use use max
and min
with dict.get
:
maximum = max(mydict, key=mydict.get) # Just use 'min' instead of 'max' for minimum.
print(maximum, mydict[maximum])
# D 87
这篇关于用键输出字典中的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文