【发布时间】:2018-11-14 23:53:20
【问题描述】:
np.NaN 的布尔值为 True。那么为什么和/或操作在 python 中表现得如此随机。
bool(np.nan) == True
如果True or np.nan 的计算结果为True,那么为什么np.nan or True 的计算结果为nan?而这对于和操作完全相反:
True and np.nan 是 nan 而np.nan and True 是 True。
【问题讨论】:
-
我无法理解您的示例和问题本身。无论如何,由于运算符优先级
x and y == z等价于x and (y == z)。因此显然与y and x == z不一样。 -
查看此 SO 帖子...stackoverflow.com/q/43925797/6361531
-
除了 Jacques 的解释外,这里称为short circuiting。例如尝试
2 or 1/0,这将返回2,因为第一个操作数是Trueish,它的值为2。但如果你改变顺序,它会引发ZeroDivisionError。
标签: python numpy null logical-operators