Home > Software design >  Finding the index of the minimum value in a list of arrays
Finding the index of the minimum value in a list of arrays

Time:12-13

Let's say I have a list with multiple arrays:

L = [
  array([-10, -8, -3, 2, 1]),
  array([-9, -4, -1, 3, 5]),
  array([-11, -5, -4, 0, 10])
]

How can I find the index of the lowest value the most efficiently?

For my example, the minimum value is -11 and the index is (2, 0), so the output should be (0, 2).

CodePudding user response:

What about the following?

import numpy as np

L = [
  np.array([-10, -8, -3, 2, 1]),
  np.array([-9, -4, -1, 3, 5]),
  np.array([-11, -5, -4, 0, 10])
]

L_2d = np.array(L)

min_index = np.unravel_index(L_2d.argmin(), L_2d.shape)
(min_index[0], min_index[1])
  • Related