使用 ~ 反转 numpy 布尔数组 [英] Inverting a numpy boolean array using ~
问题描述
我可以使用 ~A
来反转一个 numpy 布尔数组,而不是相当笨拙的函数 np.logical_and()
和 np.invert()代码>?确实,
~
似乎工作正常,但我在任何 nympy 参考手册中都找不到它,而且——更令人担忧的是——它肯定 不能 与标量一起工作(例如 bool(~True)
返回 True
!),所以我有点担心...
Can I use ~A
to invert a numpy array of booleans, instead of the rather awkward functions np.logical_and()
and np.invert()
? Indeed, ~
seems to work fine, but I can't find it in any nympy reference manual, and - more alarmingly - it certainly does not work with scalars (e.g. bool(~True)
returns True
!), so I'm a little bit worried ...
推荐答案
简答:是
参考:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html
注意:
计算输入数组中整数的底层二进制表示的按位非.这个 ufunc 实现了 C/Python 操作符 ~.
Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~.
和
bitwise_not 是 invert 的别名:
bitwise_not is an alias for invert:
>> np.bitwise_not is np.invert
>> True
这篇关于使用 ~ 反转 numpy 布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!