在numpy数组中找到一个数组?

时间:2021-05-21 21:43:17

I have a large array of ordered pairs in N-dimensions. I then have a single test array in N-dimensions, that I want to find all of the indices of its locations in the large array. A simple example looks like the following:

在n维空间中有大量有序对。然后我有一个n维的测试数组,我想找到它在大数组中的所有位置的索引。一个简单的例子如下:

>>> import numpy as np
>>> x = np.array(  ((1,2),(3,4),(5,6)) )
>>> y = np.array( (1,2) )
>>> x == y
array([[ True,  True],
   [False, False],
   [False, False]], dtype=bool)

What I want, however, is:

然而,我想要的是:

array([True,
   False,
   False], dtype=bool)

Is this possible? I want to avoid looping over the entire large array and testing all individual objects to find the indices. There are multiple locations in the large array where each test array appears, and I need all of the indices.

这是可能的吗?我希望避免对整个大数组进行循环,并测试所有单个对象以找到索引。在大数组中有多个位置,每个测试数组都会出现,我需要所有的索引。

Am I missing something simple?

我错过了什么简单的东西吗?

1 个解决方案

#1


4  

(x == y).all(axis=1)

That should do it. It tests whether all entries in each row of x == y are true and returns a 1D array of results. It's roughly equivalent to

应该做的。它测试x = y每一行中的所有条目是否为真,并返回一个一维结果数组。这是大致相当于

numpy.array([all(vector) for vector in x == y])

#1


4  

(x == y).all(axis=1)

That should do it. It tests whether all entries in each row of x == y are true and returns a 1D array of results. It's roughly equivalent to

应该做的。它测试x = y每一行中的所有条目是否为真,并返回一个一维结果数组。这是大致相当于

numpy.array([all(vector) for vector in x == y])