Reputation: 5
This is the code I've tried:
import numpy as np
a = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
if a[0] == [1, 2, 3]:
print("equal")
And this is the error I'm getting:
Exception has occurred: ValueError
The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
Upvotes: 0
Views: 210
Reputation: 13743
You could simply use numpy.array_equal
:
In [32]: if np.array_equal(a[0], [1, 2, 3]):
...: print("equal")
equal
Upvotes: 1
Reputation:
Just use np.all()
, as the error message tells you. Doing ==
on a numpy array will return an that's the same shape as the original array, only with True and False values in. They'll be True if the items at the particular index are equal, and False otherwise. .all()
will only return True
if every item in the array is True, so it will return True if the arrays are perfectly equal:
if np.all(a[0] == [1, 2, 3]):
print("equal")
Output:
equal
Upvotes: 0