jpcgandre
jpcgandre

Reputation: 1505

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

Upvotes: 4

Views: 2134

Answers (2)

Stanley Switalski
Stanley Switalski

Reputation: 43

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]

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816384

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.

Upvotes: 7

Related Questions