M.M
M.M

Reputation: 1373

The index of maximum element in an array of tuples

How can I find the index of the largest value for tuples in a list in a short number of code lines?

I could do that using two for loops but I could not make it shorter.

for example:

[(0, 0.51634575), (1, 0.113904804), (2, 0.7697494)]
[(0, 0.20560692), (1, 0.141724408), (2, -0.112972)]
[(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)]

should return

2
0
1

which are the first values (the indices) of the tuples.

here is what I tried:

a= [(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)]

max = max([x[1] for x in a])
id=-1
for x in a:
    if x[1] == max:
        id = x[0]
        break

print(id)

Upvotes: 0

Views: 1198

Answers (6)

Hugo G
Hugo G

Reputation: 16516

Baseline:

test_set = [
  [(0, 0.51634575), (1, 0.113904804), (2, 0.7697494)],
  [(0, 0.20560692), (1, 0.141724408), (2, -0.112972)],
  [(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)],
]

This is the shortest version without too much code golfing:

for test in test_set:
  c, v = zip(*test)
  result = c[v.index(max(v))]
  print(result)

And because code golfing is actually fun:

for t in test_set:print(max(t,key=lambda x:x[1])[0])

This is the most runtime and probably also most memory efficient version:

for test in test_set:
  max_elem = test[0]
  for tup in test:
    if tup[1] > max_elem[1]:
      max_elem = tup
  print(max_elem[0])

Results of all above snippets:

2
0
1

Upvotes: 0

Mykola Zotko
Mykola Zotko

Reputation: 17854

You can use the operator itemgetter() as the key in the function max():

from operator import itemgetter

l = [[(0, 0.51634575), (1, 0.113904804), (2, 0.7697494)], [(0, 0.20560692), (1, 0.141724408), (2, -0.112972)], [(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)]]

get1 = itemgetter(1)
[max(i, key=get1)[0] for i in l]
# [2, 0, 1]

Upvotes: 2

fountainhead
fountainhead

Reputation: 3722

Use enumerate:

i_max=0
for i,t in enumerate(a):
    if (a[i][1] > a[i_max][1]):
        i_max = i
print(i_max)

Without using enumerate (assuming first element of tuple always equals the index of the tuple in the list of tuples):

i_max=0
for t in a:
    if (t[1] > a[i_max][1]):
        i_max = t[0]
print(i_max)

Upvotes: 1

Camile
Camile

Reputation: 107

For one list:

b = [(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)]

n = [x[1] for x in b]
print(b[n.index(max(n))][0])

Upvotes: 1

Akhilesh_IN
Akhilesh_IN

Reputation: 1327

if data is likethis

a = [[(0, 0.51634575), (1, 0.113904804), (2, 0.7697494)],
[(0, 0.20560692), (1, 0.141724408), (2, -0.112972)],
[(0, 0.11324576), (1, 0.77262518), (2, 0.11417362)]]

then

result = [max(lis,key = lambda x :x[1])[0] for lis in a]

Upvotes: 0

thom747
thom747

Reputation: 931

It's not pretty, but this should work:

l = [(0, 0.51634575), (1, 0.113904804), (2, 0.7697494)]
l[l.index((_, max([tuple[1] for tuple in l])))][0]

Output:

2

Of course, it's debatable whether this does not constitute two for loops.

Another solution that only uses a single for loop would look like this:

max_index = -1
max_val = -1
for (index, value) in l:
    if value > max_val:
        max_index = index
        max_val = value
return max_index

Upvotes: 1

Related Questions