共计 643 个字符,预计需要花费 2 分钟才能阅读完成。
本文教程操作环境: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 技术大本营,欢送各位的到来哦~
正文完