1

I want to create an n-by-2 NumPy array, and then test whether it contains a particular 1-by-2 array (i.e. whether it contains a particular row).

Here is my code:

x = np.array([0, 1])
y = np.array([2, 3])
z = np.vstack((x, y))
if x in z:
    print "Yes"

But this gives me the error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please could somebody explain this to me? Thanks!

1 Answer 1

1

You may be using an older version of numpy since on 1.10 this does work;

>>> x = np.array([0, 1])
>>> y = np.array([2, 3])
>>> z = np.vstack((x, y))
>>> x in z
True
>>> np.__version__
'1.10.1'

That said, it does not do what you want:

>>> z
array([[0, 0],
       [0, 0]])
>>> x
array([0, 1])
>>> x in z
True

as commented out here, x in z is equivalent of (x == z).any(), not kind of row search that you want.


To implement what you need, you may do:

>>> (z == x).all(axis=1).any()
True
Sign up to request clarification or add additional context in comments.

Comments

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.