在元组的python列表中查找最大值 [英] finding maximum value in python list of tuples

查看:459
本文介绍了在元组的python列表中查找最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元组列表(列表):

I have a list of tuples (list):

('2015-06-19', 3453455, 5, 'Scheduled')
('2015-05-19', 6786788, 6, 'Overdue')
('2015-04-19', 2342344, 2, 'Not Received')
('2015-03-19', 9438549, 0, 'Not Received')
('2015-02-19', 6348759, 7, 'Not Received')

当我运行它时,我得到了:

When I run this, I get this:

>>> print(max(list))
('2015-06-19', 3453455, 5, 'Scheduled')

很明显,max(list)根据元组列表中的第一个值确定最大值.我只是想知道这是否是关于元组列表的max(list)的默认行为;只检查元组中的第一项?

Obviously, max(list) determined the max based on the first value in the list of tuples. I'm just wondering if this is the default behavior of max(list) in regards to a list of tuples; to check only the first item in the tuple?

然后,如果我想返回基于第二个项目/列的最大值的元组,该怎么办?我该怎么办?

And, what if I wanted to return the tuple with the maximum based on the second item/column. How would I do that?

推荐答案

您可以指定使用lambda进行比较的字段

You can specify which field to use for comparison using lambdas

max(list,key=lambda x:x[1])

或使用itemgetter

from operator import itemgetter
max(list,key=itemgetter(1))

这篇关于在元组的python列表中查找最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆