品易云推流 关闭
文章详情页
文章 > Python常见问题 > python中如何求取字典最大值?

python中如何求取字典最大值?

Python 字典

头像

宋雪维

2021-02-05 08:41:2025963浏览 · 0收藏 · 0评论

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

方法一:通过sorted()函数排序所有的value

import  operator
# 先通过sorted 和operator 函数对字典进行排序,然后输出value的键
classCount={"c":1,"b":4,"d":2,"e":6}
print(classCount.items())
SortedclassCount1= sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)
print(SortedclassCount1[0][0])
 
# 通过max求字典value对应的key
print(max(classCount,key=classCount.get))

方法二:通过max函数取字典中value所对应的key值

# 例:
price = {
    'a':1,
    'b':7,
    'c':5,
    'd':10,
    'e':12,
    'f':3
}
result_max = max(price,key=lambda x:price[x])
print(f'max:{result_max}')
>>>>
max:e

方法三:通过map()函数求取字典值

keys = m.keys()
keys.sort()
ma=map(m.get,keys)print ma[len(ma) - 1]

以上就是python中求取字典值的三种方法,大家可以选择自己喜欢的方法使用哦~

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图