2017-07-25 50 views
-2

我希望得到一个主numpy的2D阵列A的交叉行的索引,用另一个B.指数相交numpy的2D阵列的行

A = array([[1,2], 
      [1,3], 
      [2,3], 
      [2,4], 
      [2,5], 
      [3,4] 
      [4,5]]) 

B = array([[1,2], 
      [3,2], 
      [2,4]]) 

result=[0, -2, 3] 
##Note that the intercept 3,2 must assign (-) because it is the opposite 

当本应返回[0,-2 ,3]基于阵列A的指数。

谢谢!

回答

0

您可以参考代码。

import numpy as np 
A = np.array([[1,2], 
      [1,3], 
      [2,3], 
      [2,4], 
      [2,5], 
      [3,4], 
      [4,5]]) 

B = np.array([[1,2], 
      [3,2], 
      [2,4]]) 

result=[] 

for i in range(0, len(A)): 
    for j in range(0, len(B)): 
     if A[i].tolist() == B[j].tolist(): 
      result.append(i) 
     if A[i].tolist()[::-1] == B[j].tolist(): 
      result.append(-i) 
print(result) 

的输出是:

[0, -2, 3] 
+0

很好!但我有50个1000行的数组,这个循环变得 不可行这样的大阵,谢谢! –

0

numpy_indexed包(声明:我其作者)具有以下功能:有效地解决这样的问题。

import numpy_indexed as npi 
A = np.sort(A, axis=1) 
B = np.sort(B, axis=1) 
result = npi.indices(A, B) 
result *= (A[:, 0] == B[:, 0]) * 2 - 1 
+0

谢谢!这对我的情况非常有用,但结果都是负面的,只有倒过来的结果才会消极,我该如何解决这个问题? –

+0

应该在排序前评估== –