What is the meaning of the "in" operator between numpy arrays?

Viewed 204

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?
1 Answers

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.

Related