是否有一种 numpy 方法(并且没有 for 循环)来提取 numpy 数组中的所有索引list_of_numbers,其中值位于列表中values_of_interest?
这是我当前的解决方案:
list_of_numbers = np.array([11,0,37,0,8,1,39,38,1,0,1,0])
values_of_interest = [0,1,38]
indices = []
for value in values_of_interest:
this_indices = np.where(list_of_numbers == value)[0]
indices = np.concatenate((indices, this_indices))
print(indices) # this shows [ 1. 3. 9. 11. 5. 8. 10. 7.]
Run Code Online (Sandbox Code Playgroud)
Chr*_*ris 12
numpy.where与以下一起使用numpy.isin:
np.argwhere(np.isin(list_of_numbers, values_of_interest)).ravel()
Run Code Online (Sandbox Code Playgroud)
输出:
array([ 1, 3, 5, 7, 8, 9, 10, 11])
Run Code Online (Sandbox Code Playgroud)