python之找字典dict中的最大/小值

dogdistance = {‘dog-dog‘: 33, ‘dog-cat‘: 36, ‘dog-car‘: 41, ‘dog-bird‘: 42}

min(dogdistance, key=dogdistance.get)

返回最小值的键值: ‘dog-dog‘

max(dogdistance, key=dogdistance.get)

返回最大值的键值: ‘dog-bird‘

这里key的用法为一个函数

参考:

https://docs.python.org/3/library/functions.html#max

https://blog.csdn.net/weixin_41770169/article/details/80816886

相关推荐