[英]Dictionary with lists as values - find longest list
I have a dictionary where the values are lists.我有一本字典,其中的值是列表。 I need to find which key has the longest list as value, after removing the duplicates.
在删除重复项后,我需要找到哪个键的列表最长作为值。 If i just find the longest list this won't work as there may be a lot of duplicates.
如果我只是找到最长的列表,这将不起作用,因为可能有很多重复项。 I have tried several things, but nothing is remotely close to being correct.
我已经尝试了几件事,但没有什么是正确的。
d = # your dictionary of lists
max_key = max(d, key= lambda x: len(set(d[x])))
# here's the short version. I'll explain....
max( # the function that grabs the biggest value
d, # this is the dictionary, it iterates through and grabs each key...
key = # this overrides the default behavior of max
lambda x: # defines a lambda to handle new behavior for max
len( # the length of...
set( # the set containing (sets have no duplicates)
d[x] # the list defined by key `x`
)
)
)
Since the code for max
iterates through the dictionaries' keys (that's what a dictionary iterates through, by the by. for x in dict: print x
will print each key in dict
) it will return the key that it finds to have the highest result when it applies the function we built (that's what the lambda
does) for key=
.由于
max
的代码遍历字典的键(这是字典遍历的内容,by. for x in dict: print x
将打印dict
每个键)它将返回它找到的具有最高结果的键当它应用我们为key=
构建的函数(这就是lambda
所做的)时。 You could literally do ANYTHING here, that's the beauty of it.你可以在这里做任何事情,这就是它的美妙之处。 However, if you wanted the key AND the value, you might be able to do something like this....
但是,如果您想要键和值,则可以执行以下操作....
d = # your dictionary
max_key, max_value = max(d.items(), key = lambda k,v: len(set(v)))
# THIS DOESN'T WORK, SEE MY NOTE AT BOTTOM
This differs because instead of passing d
, which is a dictionary, we pass d.items()
, which is a list of tuples built from d
's keys and values.这是不同的,因为我们没有传递
d
,这是一个字典,而是传递d.items()
,它是一个由d
的键和值构建的元组列表。 As example:例如:
d = {"foo":"bar", "spam":['green','eggs','and','ham']}
print(d.items())
# [ ("foo", "bar"),
# ("spam", ["green","eggs","and","ham"])]
We're not looking at a dictionary anymore, but all the data is still there!我们不再看字典了,但所有数据都还在! It makes it easier to deal with using the unpack statement I used:
max_key, max_value =
.使用我使用的解包语句可以更轻松地处理:
max_key, max_value =
。 This works the same way as if you did WIDTH, HEIGHT = 1024, 768
.这与您执行
WIDTH, HEIGHT = 1024, 768
。 max
still works as usual, it iterates through the new list we built with d.items()
and passes those values to its key
function (the lambda k,v: len(set(v))
). max
仍然像往常一样工作,它遍历我们用d.items()
构建的新列表,并将这些值传递给它的key
函数( lambda k,v: len(set(v))
)。 You'll also notice we don't have to do len(set(d[k]))
but instead are operating directly on v
, that's because d.items()
has already created the d[k]
value, and using lambda k,v
is using that same unpack statement to assign the key to k
and the value to v
.您还会注意到我们不必执行
len(set(d[k]))
而是直接在v
上操作,这是因为d.items()
已经创建了d[k]
值,并且使用了lambda k,v
使用相同的解包语句将键分配给k
并将值分配给v
。
Magic!魔法! Magic that doesn't work, apparently.
显然不起作用的魔法。 I didn't dig deep enough here, and
lambda
s cannot, in fact, unpack values on their own.我在这里没有深入挖掘,实际上
lambda
s 不能自行解压缩值。 Instead, do:相反,请执行以下操作:
max_key, max_value = max(d.items(), key = lambda x: len(set(x[1])))
for less advanced user this can be a solution:对于不太高级的用户,这可以是一个解决方案:
longest = max(len(item) for item in your_dict.values())
result = [item for item in your_dict.values() if len(item) == longest]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.