我想得到一个主numpy 2d数组A的交叉行的索引,另一个得到B.
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
这应该根据数组A的索引返回[0,-2,3]。
谢谢!
答案 0 :(得分: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]
答案 1 :(得分: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