How to find the index of the absolute maximum of the first time in a list of tuples?

advertisements

Given a list of tuples:

[(-8.33958, -84.01769099999999), (-7.96129, -84.37399199999999), (-8.33958, -83.84126699999999), (-7.96129, -84.19756499999998), (-7.24097, -85.581651), (-6.86267, -85.937952), (-7.24097, -85.405227), (-6.86267, -85.76152499999999), (-9.4382, -83.473767), (-9.4382, -83.473767), (-8.35625, -85.11197399999999), (-9.0599, -83.83006499999999), (-9.0599, -83.83006499999999), (-7.97795, -85.46824499999998), (-8.35625, -84.93524699999999), (-7.97795, -85.29151799999998), (-8.33958, -85.03772699999999), (-8.00311, -88.17046199999997), (-5.88285, -86.59070369999999), (-6.86267, -88.655385), (-9.37174, -86.88101999999999), (-7.34506, -88.24291199999999), (-8.22317, -87.13259099999999), (-7.72054, -86.124309), (-8.35625, -86.131707), (-8.35625, -86.131707), (-7.03703, -88.90182), (-8.51394, -86.422992), (-8.51394, -85.83968999999999), (-9.61255, -85.510092), (-9.89311, -84.10327799999999), (-7.96129, -87.540312), (-9.13791, -86.022645)]

The aim is to find the index of the highest value of the first item in the tuple. With the example input above the output the index of the tuple (-9.89311, -84.10327799999999)

I have been doing it as such (but it doesn't return the right output):

x = [(-8.33958, -84.01769099999999), (-7.96129, -84.37399199999999), (-8.33958, -83.84126699999999), (-7.96129, -84.19756499999998), (-7.24097, -85.581651), (-6.86267, -85.937952), (-7.24097, -85.405227), (-6.86267, -85.76152499999999), (-9.4382, -83.473767), (-9.4382, -83.473767), (-8.35625, -85.11197399999999), (-9.0599, -83.83006499999999), (-9.0599, -83.83006499999999), (-7.97795, -85.46824499999998), (-8.35625, -84.93524699999999), (-7.97795, -85.29151799999998), (-8.33958, -85.03772699999999), (-8.00311, -88.17046199999997), (-5.88285, -86.59070369999999), (-6.86267, -88.655385), (-9.37174, -86.88101999999999), (-7.34506, -88.24291199999999), (-8.22317, -87.13259099999999), (-7.72054, -86.124309), (-8.35625, -86.131707), (-8.35625, -86.131707), (-7.03703, -88.90182), (-8.51394, -86.422992), (-8.51394, -85.83968999999999), (-9.61255, -85.510092), (-9.89311, -84.10327799999999), (-7.96129, -87.540312), (-9.13791, -86.022645)]

index_of_max_abs_j = -1
for i, (j,k) in enumerate(x):
    if j*j > index_of_max_abs_j:
        index_of_max_abs_j = i
print index_of_max_abs_j

The code returns the index of max(j*j) but is that right? Is it different from trying to find max(|j|)?

But is there another way of achieving the same output? Maybe with sorted and reverse and key with some math.abs? Is the alternative with sorted, reverse and/or key more efficient?

If there's any item in the list of tuples that has the same value, return the first index of the first instance of the maximum absolute value.


I'm not sure why you're comparing the value of the first element of the tuple to the index, but this gets you the index of the tuple with the highest absolute value for the first element.

max_index = -1
max_value = 0
for i, z in enumerate(x):
    value = abs(z[0])
    if value > max_value:
        max_index = i
        max_value = value

print(x[max_index])

Or as a less readable one-liner,

print(x.index(max(x, key=lambda y:abs(y[0]))))