1

I am trying to check if a 1 dimensional numpy array is a part of a bigger, 2 dimensional array. I could do this with many for-loops but I guess there is a more pythonic way to do so.

The attempt I have to far:

1darray = np.array([0,0,0])
2darray = np.array([[0,0,1],[0,1,0],[1,0,0]]) 
1darray in 2darray 

But this code returns a True , as long as one of the elements from 1darray occures somewhere in 2darray. But I want to check if the whole array is a row in the bigger one, so I would want this code to return False, while I would want this code to return True:

1darray = np.array([0,0,1])
2darray = np.array([[0,0,1],[0,1,0],[1,0,0]]) 
1darray in 2darray 

I would appreciate any help, Thanks already!

1

1 Answer 1

5

You can use any() and all() function to achieve this.

>>> a=np.array([0,0,0])
>>> b=np.array([[0,0,1],[0,1,0],[1,0,0]])
>>> (a == b).all(axis=1).any()
False

>>> a=np.array([0,0,1])
>>> b=np.array([[0,0,1],[0,1,0],[1,0,0]])
>>> (a == b).all(axis=1).any()
True
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was exactly what I was looking for.

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.