Python: 如何查找数组中特定元素的索引?

Python: How to find index of a specific element in an array?

鉴于此,我有以下数组:

 import numpy as np

 dt = np.array([1,2,3,4,5,2,1,3])

我可以通过以下代码 select 值小于 3 的单元格:

print(dt[dt<3])

但是,我怎样才能获得 selected 单元格的索引?

我最喜欢的结果是:

[0,1,5,6]

我不确定你是否需要 numpy

lst = [1, 2, 3, 4, 5, 2, 1, 3]
indexes = [i for i, v in enumerate(lst) if v < 3]

尝试

x = np.array([1,2,3,4,5,2,1,3])
np.where(x<3)

输出:

(array([0, 1, 5, 6], dtype=int64),)

您将获得所有正确的索引。