Get the max value on a list of tuples

2024/11/20 15:37:20

I have a list of tuples:

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

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.

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

  • (2, (1, S)) = Ace of Spades with an index of 2.
  • (0, (12, H)) = King of Hearts with an index of 0
  • (1, (5, C)) = 5 of Clubs with index 1

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))

My attempt is:

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?

Thanks in advance.

Answer

replace

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

with

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

Demo

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))

Output:

(0, (12, 'H'))
(1, (5, 'C'))
https://en.xdnf.cn/q/119818.html

Related Q&A

Naming of file while saving with extension

How can I save a file with the name as: oldname+"_new"+extension in an elegant way? I currently do: ext = os.path.splitext(file)[1] output_file = (root+/+ os.path.splitext(file)[0]+"_ne…

Unique permutations of a list without repetition

I understand there are many, many posts about permutations (unique, variable length, etc.), but I have not been able to use them to solve my particular issue.Lets say I have a list of metropolitan area…

Join Operation for Dictionary in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to nest a list based on increasing sequences and ignore left overlapping ranges

This is my input mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]from 2 to 11, its increasing, so we need to grab the min max [2, 11] from 7 to 10 its increasing, but we need to ignore it because the …

Float comparison (1.0 == 1.0) always false

Im using the following function in Python 2.7.3 and Kivy 1.8.0 to fade-in a Grid widget:def __init__(self, **kwargs):# ...Init parent class here...self.grid.opacity = 0.0Clock.schedule_interval(self.sh…

How to I extract objects? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 2…

sklearn.metrics.roc_curve only shows 5 fprs, tprs, thresholds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 2 years ago.Improve…

I can not transform a file to a dictionary in python [duplicate]

This question already has answers here:ValueError: need more than 1 value to unpack python(4 answers)Closed 5 years ago.I am trying to transform a file to dictionary but having error.def txt_to_dict():…

Loan payment calculation

I am learning Python and am stuck. I am trying to find the loan payment amount. I currently have:def myMonthlyPayment(Principal, annual_r, n):years = nr = ( annual_r / 100 ) / 12MonthlyPayment = (Princ…

How can I implement this model?

Problem statement I have 3 classes (A, B, and C). I have 6 features: train_x = [[ 6.442 6.338 7.027 8.789 10.009 12.566][ 6.338 7.027 5.338 10.009 8.122 11.217][ 7.027 5.338 5.335 8.122 5.537…