Is there a numpy function to find an array in multi dimensional array?

Viewed 50

I have a numpy array with n row and p columns.

I want to check if a given row is in my array and find the index.

For exemple I have a numpy array like this :

[[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0],....]

I want to check if this array [6,0,5,8,2,1] is in my numpy array or and where.

Is there a numpy function for that ?

I'm sorry for asking naive question but I'm quite confuse right now.

1 Answers

You can use == and .all(axis=1) to match entire rows, then use numpy.where() to get the index:

import numpy as np

a = np.array([[1,0,8,7,2,2],[1,3,7,0,3,0],[1,7,1,0,1,0],[1,9,1,0,6,0],[1,8,1,7,9,0], [6,0,5,8,2,1]])
b = np.array([6,0,5,8,2,1])

print(np.where((a==b).all(axis=1)))

Output:

(array([5], dtype=int32),)
Related