2

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 Answer 1

2

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),)
Sign up to request clarification or add additional context in comments.

2 Comments

I recommend to try and break up the expression to print each sub-part of it to understand the process better
thank you that the thing I was searching !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.