Python/numpy:获取值列表的数组位置

Jos*_*hDG 3 python numpy scipy

假设我有一个值数组,nameArr = ['josh','is','a','person']我想要一个像arrayLocation(nameArr,['a','is'])返回的函数[2 1].

该功能是否已经存在,如果不存在,我该如何有效地实现它?

luk*_*ell 6

使用numpy.where

In [17]: nameArr = np.array(['josh','is','a','person'])

In [18]: [np.where(nameArr==i) for i in ['a','is']]
Out[18]: [(array([2]),), (array([1]),)]
Run Code Online (Sandbox Code Playgroud)