且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

列为字典中的值,获取最长列表的键

更新时间:2023-08-26 13:51:46

使用max:

>>> max(testDict, key=lambda x:len(testDict[x]))
32

如果多个键包含最长的列表:

If multiple keys contain the longest list:

那我想获取多个密钥.

I want to get multiple keys then.

>>> testDict = {76: [4], 32: [2, 4, 7, 3], 56: [2, 58, 59], 10: [1, 2, 3, 4]}
>>> mx = max(len(x) for x in testDict.itervalues())
>>> [k for k, v in testDict.iteritems() if len(v)==mx]
[32, 10]