我有一本字典: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/