获取字典python中每个键的最大值

我有以下字典,我想输出每个键的最大值:

yo = {'is': [1, 3, 4, 8, 10],
             'at': [3, 10, 15, 7, 9],
             'test': [5, 3, 7, 8, 1],
             'this': [2, 3, 5, 6, 11]}

例如,输出应该是这样的

[10, 15, 8, 11]
or 
['is' 10, 'at' 15, 'test' 8, 'this' 11]

回答

使用list comprehension

result = [max(v) for k,v in yo.items()]
# PRINTS [10, 15, 8, 11]

dict comprehension

result_dict = {k:max(v) for k,v in yo.items()}
# Prints {'is': 10, 'at': 15, 'test': 8, 'this': 11}

  • 非常感谢!

以上是获取字典python中每个键的最大值的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>