What is the meaning of the “in” operator between numpy arrays?

非 Y 不嫁゛ 提交于 2021-02-07 19:49:50

问题


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:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!