[Numpy-discussion] What should np.ndarray.__contains__ do
Mon Feb 25 09:10:16 CST 2013
currently the `__contains__` method or the `in` operator on arrays, does
not return what the user would expect when in the operation `a in b` the
`a` is not a single element (see "In -" below).
The first solution coming to mind might be checking `all()` for all
dimensions given in argument `a` (see line "In " for a simplistic
example). This does not play too well with broadcasting however, but one
could maybe simply *not* broadcast at all (i.e. a.shape ==
b.shape[b.ndim-a.ndim:]) and raise an error/return False otherwise.
On the other hand one could say broadcasting of `a` onto `b` should be
"any" along that dimension (see "In "). The other way should maybe
raise an error though (see "In " to understand what I mean).
I think using broadcasting dimensions where `a` is repeated over `b` as
the dimensions to use "any" logic on is the most general way for numpy
to handle this consistently, while the other way around could be handled
with an `all` but to me makes so little sense that I think it should be
an error. Of course this is different to a list of lists, which gives
False in these cases, but arrays are not list of lists...
As a side note, since for loop, etc. use "for item in array", I do not
think that vectorizing along `a` as np.in1d does is reasonable. `in`
should return a single boolean.
I have opened an issue for it:
In : a = np.array([0, 2])
In : b = np.arange(10).reshape(5,2)
In : b
In : a in b
In : (b == a).any()
In : (b == a).all(0).any() # the 0 could be multiple axes
In : a_2d = a[None,:]
In : a_2d in b # broadcast dimension means "any" -> True
In : [0, 1] in b[:,:1] # should not work (or be False, not True)
More information about the NumPy-Discussion