3

I noticed that in can be used between numpy arrays. However its meaning can be a bit counter-intuitive.

import numpy as np
np.array([0]) in np.array([1, 2])
# False
np.array([0]) in np.array([0, 1])
# True
np.array([0, 1]) in np.array([0])
# True -- somewhat surprisingly

So it seems that it behaves like np.any(np.isin(·, ·)) rather than the somewhat more intuitive np.all(np.isin(·, ·)).

  1. Is this really the case?
  2. What is the rationale behind this choice?
user209974
  • 1,737
  • 2
  • 15
  • 31
  • you should as numpy's authors. Maybe they use `any` because it was more useful or more intuitive for authors. – furas Oct 07 '19 at 10:21
  • Possible duplicate of [How does \_\_contains\_\_ work for ndarrays?](https://stackoverflow.com/questions/18320624/how-does-contains-work-for-ndarrays) – Joanna Oct 07 '19 at 13:23
  • @Joanna thanks, that confirms my observation, but I would still like to hear about the reasons (or lack thereof) behind this choice. – user209974 Oct 08 '19 at 14:30

1 Answers1

0

As the "in" operator behaviour is defined by the implementation of the contains method of np.array class, you should check this answer https://stackoverflow.com/a/30690604/7533781 - it is very well explained.

Following this definition also [0, 1] in np.array([0]) evaluates to True - you can try it yourself.

Joanna
  • 271
  • 1
  • 9