python - 获取字典中某些值中最大值的键

标签 python dictionary lambda

我有一本字典:D = {'N':5, 'S':0, 'W':6, 'E':1}我想获得 D['N'] 中最大值的键和D['S'] .

例如,我尝试了代码 print(lambda k: max(k['N'], k['S'])(k=D.keys()))但它返回像这样的 lambda 对象 <function <lambda> at 0x000002C7B060C1E0> 。虽然我想在输出中得到N

需要帮助。谢谢!

最佳答案

只需将 dict.get 函数作为 max() 中的 key 参数传递即可:

# to find the max of entire dictionary

max(D, key=D.get)
# 'W'

# to find individual keys

max(['N', 'S'], key=D.get)
# 'N'

关于python - 获取字典中某些值中最大值的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090718/

相关文章:

python - OpenCV VideoWriter错误 "dimensions too large for MPEG-4"

python - 我如何洗牌有约束的列表(1 和 2、3 和 4、5 和 6 不相邻)?

c++ - 将整数映射到数组

dictionary - 如何在 clojure 中检查 map 中的重复项?

c# - Linq - 如何选择一个新对象来包含其他对象的列表?

python - 使用 lambda 的 Telegram 机器人 message_handler

python - App Engine 反序列化 python 中的记录 : is it really this slow?

python - 使用 Python Requests 在请求之间设置新的 cookie

swift - 什么是 Swift 中用于搜索应用程序的字典的可行替代方案?

c++ - 支持 C++14 lambda 的 ros 包