Reputation: 58895
With the following arrays of objects:
a = np.array([[1], [1, 2], [1, 2, 3], [1], [1]], dtype=object)
b = np.array([(1,), (1, 2), (1, 2, 3), (1,), (1,)], dtype=object)
The following equality checks do not work:
a==[1]
#array([False, False, False, False, False], dtype=bool)
b==(1,)
#array([False, False, False, False, False], dtype=bool)
if I use strings instead:
c = np.array(['[1]', '[1, 2]', '[1, 2, 3]', '[1]', '[1]'])
the equality check works:
c == '[1]'
#array([ True, False, False, True, True], dtype=bool)
why the array check behaves like that?
If we iterate over a or b and perform the checks it also gives the expected result:
[i==[1] for i in a]
#[True, False, False, True, True]
[i==(1,) for i in b]
#[True, False, False, True, True]
Thank you!
Upvotes: 4
Views: 574
Reputation: 280564
NumPy is designed to automatically treat array-like objects as arrays in many situations. Here, NumPy sees that [1]
and (1,)
are array-like objects and applies the broadcasting rules. Length-1 axes on either side are expanded out to the length of the other object's corresponding axis, and if one object has less dimensions than the other, missing dimensions are filled in on the left with the other object's lengths in those dimensions. Thus,
a == [1]
gives the same result as
a == numpy.array([1, 1, 1, 1, 1])
which is an array of 5 False
s.
Upvotes: 6