我知道Python列表有一个方法可以返回某个对象的第一个索引:
>>> xs = [1, 2, 3]
>>> xs.index(2)
1
NumPy数组也有类似的东西吗?
我知道Python列表有一个方法可以返回某个对象的第一个索引:
>>> xs = [1, 2, 3]
>>> xs.index(2)
1
NumPy数组也有类似的东西吗?
当前回答
是的,给定一个数组,数组和一个值,要搜索的项,你可以使用np。的地方:
itemindex = numpy.where(array == item)
结果是一个元组,首先是所有的行索引,然后是所有的列索引。
例如,如果一个数组是二维的,它包含你的项目在两个位置,那么
array[itemindex[0][0]][itemindex[1][0]]
将等于你的项目,因此将是:
array[itemindex[0][1]][itemindex[1][1]]
其他回答
如果你想用它作为其他东西的索引,如果数组是可广播的,你可以使用布尔索引;不需要显式索引。要做到这一点,绝对最简单的方法是基于真值进行索引。
other_array[first_array == item]
任何布尔运算都可以:
a = numpy.arange(100)
other_array[first_array > 50]
非零方法也接受布尔值:
index = numpy.nonzero(first_array == item)[0][0]
两个0分别表示索引元组(假设first_array是1D)和索引数组中的第一项。
是的,给定一个数组,数组和一个值,要搜索的项,你可以使用np。的地方:
itemindex = numpy.where(array == item)
结果是一个元组,首先是所有的行索引,然后是所有的列索引。
例如,如果一个数组是二维的,它包含你的项目在两个位置,那么
array[itemindex[0][0]][itemindex[1][0]]
将等于你的项目,因此将是:
array[itemindex[0][1]][itemindex[1][1]]
对于1D数组,我推荐np。平坦非零(array == value)[0],它等价于np。非零(array == value)[0][0]和np。其中(array == value)[0][0],但避免了对一个单元素元组开箱的丑陋。
8种方法的比较
TL; diana:
(注:适用于100M元素以下的1d数组)
为了获得最佳性能,请使用index_of__v5 (numba + numpy. 5)。枚举+ for循环;参见下面的代码)。 如果numba不可用: 如果期望在前100k个元素中找到目标值,请使用index_of__v7 (for循环+枚举)。 否则使用index_of__v2/v3/v4 (numpy. exe)。Argmax或numpy。基于flatnonzero)。
由perfplot提供
import numpy as np
from numba import njit
# Based on: numpy.argmax()
# Proposed by: John Haberstroh (https://stackoverflow.com/a/67497472/7204581)
def index_of__v1(arr: np.array, v):
is_v = (arr == v)
return is_v.argmax() if is_v.any() else -1
# Based on: numpy.argmax()
def index_of__v2(arr: np.array, v):
return (arr == v).argmax() if v in arr else -1
# Based on: numpy.flatnonzero()
# Proposed by: 1'' (https://stackoverflow.com/a/42049655/7204581)
def index_of__v3(arr: np.array, v):
idxs = np.flatnonzero(arr == v)
return idxs[0] if len(idxs) > 0 else -1
# Based on: numpy.argmax()
def index_of__v4(arr: np.array, v):
return np.r_[False, (arr == v)].argmax() - 1
# Based on: numba, for loop
# Proposed by: MSeifert (https://stackoverflow.com/a/41578614/7204581)
@njit
def index_of__v5(arr: np.array, v):
for idx, val in np.ndenumerate(arr):
if val == v:
return idx[0]
return -1
# Based on: numpy.ndenumerate(), for loop
def index_of__v6(arr: np.array, v):
return next((idx[0] for idx, val in np.ndenumerate(arr) if val == v), -1)
# Based on: enumerate(), for loop
# Proposed by: Noyer282 (https://stackoverflow.com/a/40426159/7204581)
def index_of__v7(arr: np.array, v):
return next((idx for idx, val in enumerate(arr) if val == v), -1)
# Based on: list.index()
# Proposed by: Hima (https://stackoverflow.com/a/23994923/7204581)
def index_of__v8(arr: np.array, v):
l = list(arr)
try:
return l.index(v)
except ValueError:
return -1
去Colab
index_lst_form_numpy = pd.DataFrame(df).reset_index()["index"].tolist()