找到具有最大值的键并打印其值(字典,python)

sor*_*ted 1 python sorting dictionary

假设我们有一个字典,

   dict = {'blue': ['sky','sea'], 'orange': ['carrots','sunset','oranges'], 'green': ['grass']}
Run Code Online (Sandbox Code Playgroud)

输出应该是

['carrots','sunset','oranges']
Run Code Online (Sandbox Code Playgroud)

因为它获得了最多的值.

这是我到目前为止:

for k,v in dict.items():
    print(max(k,len(v)))
Run Code Online (Sandbox Code Playgroud)

Ter*_*ryA 9

为什么不呢:

>>> d = {'blue': ['sky','sea'], 'orange': ['carrots','sunset','oranges'], 'green': ['grass']}
>>> print max(d.values(), key=len)
['carrots', 'sunset', 'oranges']
Run Code Online (Sandbox Code Playgroud)

最好不要为字典命名dict.这将覆盖内置类型.