Python Numpy:搜索第一个匹配行

Python Numpy:搜索第一个匹配行,python,arrays,numpy,Python,Arrays,Numpy,如果我有这样一个numpy数组: import numpy as np x = np.array([[0,1],[0,2],[1,1],[0,2]]) 如何返回与[0,2]匹配的第一行的索引 对于列表,很容易使用索引: [[0,1],[0,2],[1,1],[0,2]] l.index([0,2]) > 1 我知道numpy具有numpy.where功能,但我不确定如何利用numpy的输出。where: np.where(x==[0,2]) > (array([0, 1, 1,

如果我有这样一个numpy数组:

import numpy as np
x = np.array([[0,1],[0,2],[1,1],[0,2]])
如何返回与
[0,2]
匹配的第一行的索引

对于列表,很容易使用
索引

[[0,1],[0,2],[1,1],[0,2]]

l.index([0,2])
> 1
我知道
numpy
具有
numpy.where
功能,但我不确定如何利用
numpy的输出。where

np.where(x==[0,2])
> (array([0, 1, 1, 3, 3]), array([0, 0, 1, 0, 1]))
还有
numpy.argmax
,但它也没有返回我要查找的内容,即索引
1

np.argmax(x == [0,2], axis = 1)

如果搜索列表是
[0,2]
,那么当与
x
进行比较时,您会带来一个与
x
形状相同的掩码。由于您正在查找精确匹配,因此将查找具有所有
TRUE
值的行。最后,您需要第一个索引,因此使用或并选择第一个元素。作为示例运行的实现将是-

In [132]: x
Out[132]: 
array([[0, 1],
       [0, 2],
       [1, 1],
       [0, 2]])

In [133]: search_list = [0,2]

In [134]: np.where((x == search_list).all(1))[0][0]
Out[134]: 1

In [135]: np.nonzero((x == search_list).all(1))[0][0]
Out[135]: 1