How do I find the index of the lowest value in Python?

299 views Asked by At

So I found the lowest value in my array using val = np.argmin(R_abs) in line 50, but now I'm trying to find the index of that value. I tried using val_index = [idx for idx, minval in enumerate(R_abs) if minval == val] in line 52, but I get an empty array. What am I doing wrong? I believe .list() only works for list. R_abs is an array.

R_abs = abs(R-atm_tropo)
val = np.argmin(R_abs) # (line 50)
# val_index = np.where(R_abs == val)
val_index = [idx for idx, minval in enumerate(R_abs) if minval == val] # (line 52)
1

There are 1 answers

2
Cargo23 On

Updated for NumPy Array instead of Python List:

This article suggests that you use np.where:

import numpy as np
# Create a numpy array from a list of numbers
arr = np.array([11, 12, 13, 14, 15, 16, 17, 15, 11, 12, 14, 15, 16, 17])# Get the index of elements with value 15
result = np.where(arr == 15)
print('Tuple of arrays returned : ', result)
print("Elements with value 15 exists at following indices", result[0], sep='\n')

Returns

Tuple of arrays returned :  (array([ 4,  7, 11], dtype=int32),)
Elements with value 15 exists at following indices
[ 4  7 11]