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

2025-12-01 0 68,138

本文教程操作环境: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中求取字典值的三种方法,大家可以选择自己喜欢的方法使用哦~

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 python基础 python中如何求取字典最大值? https://www.zhiup.top/1607.html

相关