python 找列表中的最大值,在元组的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')

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?

解决方案

You can specify which field to use for comparison using lambdas

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

or alternatively, using itemgetter

from operator import itemgetter

max(list,key=itemgetter(1))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值