alvas
alvas

Reputation: 122152

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

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.

Upvotes: 1

Views: 3446

Answers (4)

Suresh Mali
Suresh Mali

Reputation: 348

You could do it in 2 steps get the max based on first key in step 1, then get index with the element

>>> max_value = sorted(x, key=lambda y: y[0])[0]

>>> x.index(max_value)

30

Upvotes: 0

Xavier Combelle
Xavier Combelle

Reputation: 11235

print(max((abs(value[0]),index) for (index,value) in enumerate(values))[1]

would give the index of the last element having the max element in one pass with native function so should be the fastest way

If you really want to have the first of max elements you can do

print(-max((abs(value[0]),-index) for (index,value) in enumerate(values))[1])

Upvotes: 1

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

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

Upvotes: 3

Riccati
Riccati

Reputation: 461

Here is an alternative, offered without comment on efficiency:

y = [abs(item[0]) for item in x]
m = max(y)
print(y.index(m))

Upvotes: 2

Related Questions