获取元组列表中的最大值 [英] Get the max value on a list of tuples

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

问题描述

我有一个元组列表:

card_list= [(2, (1, S)), (0, (12, H)), (1, (5, C)]

此列表包含卡片:(cardindex,(value,suit)),其中cardindex是存储卡片位置的索引,但与我的特定问题无关.

This list contains cards: (cardindex, (value, suit)) where cardindex is a index to store the position of the card but irrelevant for this my particular question.

因此,在示例中,列表中有3张卡片:

So in the example there are 3 cards in the list:

  • (2,(1,S))=索引为2的黑桃王牌.
  • (0,(12,H))=国王索引为0的Hearts
  • (1,(5,C))= 5个索引为1的俱乐部

好吧,我的问题是:我希望获得具有最大值的物品,这就是我必须获得的物品:(0,(12,H))

Well, my question is: I desire to obtain the item with the max value, this is, i have to get the item: (0, (12, H))

我的尝试是:

CardWithHighestValue= max(card_list,key=itemgetter(1)[0])

但是我得到了物品或价值?最重要的是:那句话真的正确吗?

But I get the item or the value? And the most important: is it really correct that sentence?

谢谢.

推荐答案

替换

CardWithHighestValue= max(card_list,key=itemgetter(1)[0])

使用

CardWithHighestValue= max(card_list,key=itemgetter(1))

演示

from operator import itemgetter
card_list= [(2, (1, "S")), (0, (12, "H")), (1, (5, "C"))]
print max(card_list,key=itemgetter(1)) 

card_list= [(2, (1, "S")), (0, (4, "H")), (1, (5, "C"))]
print max(card_list,key=itemgetter(1))

输出:

(0, (12, 'H'))
(1, (5, 'C'))

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

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