简体   繁体   中英

Maximum value of a tuple

I've 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. So I use:

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

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

Cheers

Joao

You can pass a function as key argument 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 for the calculation.

Reference : max , operator.itemgetter


†: Similar to how sort and sorted work, that's why the information in the Sorting HowTo might be relevant as well.

Just to tack onto Felix Kling's answer, which I can't thank you enough for, if you would like to single out a max number in the tuple, add ['index'] on to the end of Felix Kling's code. For my application I needed the max number which resided in the second index of each tuple. My code was:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM