0
如何查找兩個numpy數組之間精確匹配的行的索引。例如:查找兩個numpy數組中匹配行的索引
x = np.array(([0,1],
[1,0],
[0,0]))
y = np.array(([0,1],
[1,1],
[0,0]))
這應返回:
matches = [0,2] # Match at row no 0 and 2
如何查找兩個numpy數組之間精確匹配的行的索引。例如:查找兩個numpy數組中匹配行的索引
x = np.array(([0,1],
[1,0],
[0,0]))
y = np.array(([0,1],
[1,1],
[0,0]))
這應返回:
matches = [0,2] # Match at row no 0 and 2
np.flatnonzero((x == y).all(1))
# array([0, 2])
或:
np.nonzero((x == y).all(1))[0]
或:
np.where((x == y).all(1))[0]
這適用於每對numpy陣列,如果長度相同:
matches = [i for i in range(len(x)) if x[i].tolist()==y[i].tolist()]