如何在Python中找到最小值的索引?

2024-09-29 23:24:41 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我在第50行中使用val = np.argmin(R_abs)找到了数组中的最低值,但现在我试图找到该值的索引。我尝试在第52行中使用val_index = [idx for idx, minval in enumerate(R_abs) if minval == val],但得到了一个空数组。我做错了什么?我相信.list()只适用于list。R_abs是一个数组

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)

Tags: inforindexifnplinevalabs
1条回答
网友
1楼 · 发布于 2024-09-29 23:24:41

更新了NumPy数组而不是Python列表:

This article建议您使用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')

返回

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

相关问题 更多 >

    热门问题