![](/img/trans.png)
[英]How to get the item with max date value in a dictionary with mixed type values?
[英]How to give the max item of a dictionary given a value somewhere in an array in the value?
在python中,说我有字典
{1:[1,333], 2:[5,22], 3:[8:0]}
我想通过值数组中的第二项返回字典中的最大项,因此在这种情况下,333击败了所有剩余数字在同一位置,我希望函数可以返回
(1,[1,33])
是否有任何巧妙的方法可以将max()
与key或其他方法一起使用?
>>> d = {1:[1,333], 2:[5,22], 3:[8, 0]}
>>> max(d.items(), key=lambda e: e[1][1])
(1, [1, 333])
您可以使用max()
的key
参数来实现这一点,即:
d = {1:[1,333], 2:[5,22], 3:[8,0]}
# Use d[k][1] so that max is based on the second
# item in each list
max_key = max(d, key=lambda k: d[k][1])
print((max_key, d[max_key]))
(1, [1, 333])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.