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

noo*_*cel 0 python dictionary key list python-3.x

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

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]}
Run Code Online (Sandbox Code Playgroud)

例如,输出应该是这样的

[10, 15, 8, 11]
or 
['is' 10, 'at' 15, 'test' 8, 'this' 11]
Run Code Online (Sandbox Code Playgroud)

Nk0*_*k03 7

使用list comprehension

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

dict comprehension

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

  • 非常感谢! (2认同)