The maximum value of the tuple

I have this tuple:

lpfData = ((0.0, 0.0), (0.100000001490116, 0.0879716649651527), ..., (1.41875004768372, 0.481221735477448),..., (45.1781234741211, 0.11620718985796)) 

and I want to find the maximum value of the second column. Therefore, I use:

 maxLPFt = max(lpfData) maxLPF = maxLPFt[1] 

But I always get the value of the second column in combination with the maximum value of the first column. Basic stuff, but google didn't help.

Greetings

Joao

+4
source share
2 answers

You can pass the function as an argument to key to extract the value you want to compare :

 import operator maxLPFt = max(lpfData, key=operator.itemgetter(1)) 

This will use the second element of each tuple to compute.

Link : max , operator.itemgetter


†. Similar to how sort and sorted , so the information in Sorting HowTo can also matter.

+7
source

Just to answer Felix Kling's answer, which I cannot thank you if you want to allocate the maximum number in a tuple, add ['index'] at the end of the Felix Kling code. For my application, I needed the maximum number that was in the second index of each tuple. My code is:

 maxLPFt = max(lpfData, key=operator.itemgetter(1))[1] 
0
source

Source: https://habr.com/ru/post/1401045/


All Articles