&vs * 和 |对比 + [英] & vs * and | vs +

查看:58
本文介绍了&vs * 和 |对比 +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行 boolean 数组比较时,使用 & 代替 *|<是否有任何优势/约定/code> 代替 +?这些总是等价的吗?

When doing boolean array comparison, is there any advantage / convention to using & in place of * or | in place of +? Are these always equivalent?

(如果这些在文档中,链接可能是可以接受的答案,但我对numpy &"和numpy elementwise boolean compare"的天真搜索没有产生任何相关的东西)

(if these are in the documentation, a link would probably be an acceptable answer, but my naive search for 'numpy ampersand' and 'numpy elementwise boolean comparison' didn't yield anything relevant)

推荐答案

在 numpy 中 &| 等价于 np.bitwise_andnp.bitwise_or.您还可以将 ^ 用于 np.bitwise_xor.这都记录在算术和比较运算ndarray 文档的部分.np.logical_and 也有 ufunc,np.logical_ornp.logical_xor.

In numpy & and | are equivalent to np.bitwise_and and np.bitwise_or. You can also use ^ for np.bitwise_xor. This is all documented in the Arithmetic and comparison operations section of the ndarray docs. There are also ufuncs for np.logical_and, np.logical_or and np.logical_xor.

如果你的数组都是 dtype bool 应该没有任何区别.我个人倾向于 &|,即使你对 bool dtype 不严格,它也会给你带来这样的麻烦:

If your arrays are all of dtype bool there shouldn't be any difference. I personally lean towards & and |, even though if you are not strict about the bool dtype it can get you in troubles like this:

In [30]: np.array(2) & np.array(1)
Out[30]: 0

这篇关于&amp;vs * 和 |对比 +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆