问题
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(·, ·))
.
- Is this really the case?
- What is the rationale behind this choice?
回答1:
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.
来源:https://stackoverflow.com/questions/58266629/what-is-the-meaning-of-the-in-operator-between-numpy-arrays