New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contains method is not consistent for subarrays #3016
Comments
This might be worth raising on the list. |
An example of this giving a meaningless result: >>> np.arange(3) in np.eye(3)
True Implementing one option from the mailing list (@seberg's):
Gives us: def __contains__(self, other):
other = np.asanyarray(other)
ndim = min(self.ndim, other.ndim)
eq = self == other
matched_axes = []
for ax in range(-ndim, 0):
if other.shape[ax] == self.shape[ax]:
matched_axes.append(ax)
elif other.shape[ax] > self.shape[ax]:
return False
return eq.all(axis=matched_axes).any() Which looks to me like a sensible variant of option 3 |
I personally think we should go with the "item must be an element" solution, then maybe create a fancy function for more complex stuff. |
Would you be able to link to the past mailing list discussion? Either way, if we're going to change behaviour, we'd need to pass through the single-element-or- |
http://numpy-discussion.10968.n7.nabble.com/What-should-np-ndarray-contains-do-td32964.html I guess this is the one from that time, I really don't remember much about where it went, maybe I was just to lazy to do it, or a bit too confused about the object array case and then lost interest. |
Better link probably: https://mail.python.org/pipermail/numpy-discussion/2013-February/065572.html |
Thanks - updated the top post with that, and found the suggestion in that thread that matches my implementation. |
I would also only support single elements, and raise an error for higher dimensional keys. It is hard to understand the current behavior as anything other than a bug. |
The
__contains__
method is written to be used for a single array element. However for example in list of list,__contains__
does a check more equivalent to subarrays.in
must return a single boolean.After some discussion on the list (nabble), there are three main possibilities:
a
fora in b
will normally be a simple error. (As nathaniel mentioned on the list).in
operates on the first dimension.Point 2. seems wrong, since arrays are not list of lists. Point 3. has some merit, it can go as far as allowing things similar to strings
'a' in 'cat'
, however there are some problems with the details. Point 1. is the simplest and safest solution. One problem with 2. is that for object arrays it can be not quite clear how to interpret for example a tuple/list.At this time (there was not much discussion yet though), it seems that the best solution is to just raise an error (i.e. solution 1.). Finding subarrays is better suited for a dedicated function.
The text was updated successfully, but these errors were encountered: