“和"(布尔值)与“&" (按位)-为什么列表与numpy数组在行为上有所不同? [英] 'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?

查看:134
本文介绍了“和"(布尔值)与“&" (按位)-为什么列表与numpy数组在行为上有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么解释了列表与NumPy数组上布尔运算和按位运算的行为差异?

我对在Python中正确使用&and感到困惑,如以下示例所示.

I'm confused about the appropriate use of & vs and in Python, illustrated in the following examples.

mylist1 = [True,  True,  True, False,  True]
mylist2 = [False, True, False,  True, False]

>>> len(mylist1) == len(mylist2)
True

# ---- Example 1 ----
>>> mylist1 and mylist2
[False, True, False, True, False]
# I would have expected [False, True, False, False, False]

# ---- Example 2 ----
>>> mylist1 & mylist2
TypeError: unsupported operand type(s) for &: 'list' and 'list'
# Why not just like example 1?

>>> import numpy as np

# ---- Example 3 ----
>>> np.array(mylist1) and np.array(mylist2)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
# Why not just like Example 4?

# ---- Example 4 ----
>>> np.array(mylist1) & np.array(mylist2)
array([False,  True, False, False, False], dtype=bool)
# This is the output I was expecting!

此答案这个答案帮助我理解了 and 是布尔操作,但 & 是按位运算.

This answer and this answer helped me understand that and is a boolean operation but & is a bitwise operation.

我阅读了有关按位操作的信息,以更好地理解该概念,但是我正在努力使用该概念信息可以理解我上面的四个示例.

I read about bitwise operations to better understand the concept, but I am struggling to use that information to make sense of my above 4 examples.

示例4使我达到所需的输出,这很好,但是对于何时/如何/为什么应该使用and vs &仍然感到困惑.为什么列表和NumPy数组在这些运算符上的行为不同?

Example 4 led me to my desired output, so that is fine, but I am still confused about when/how/why I should use and vs &. Why do lists and NumPy arrays behave differently with these operators?

谁能帮助我理解布尔运算和按位运算之间的区别,以解释为什么它们对列表和NumPy数组的处理方式不同?

推荐答案

and测试两个表达式在逻辑上是否是True,而&(与True/False值一起使用时)测试两个表达式是否都在逻辑上True.

and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.

在Python中,通常将空的内置对象在逻辑上视为False,而将非空的内置对象在逻辑上视为True.这可以简化常见的用例,在这种情况下,如果列表为空,则要执行某项操作;如果列表为空,则要执行其他操作.请注意,这意味着列表[False]在逻辑上是True:

In Python, empty built-in objects are typically treated as logically False while non-empty built-ins are logically True. This facilitates the common use case where you want to do something if a list is empty and something else if the list is not. Note that this means that the list [False] is logically True:

>>> if [False]:
...    print 'True'
...
True

因此,在示例1中,第一个列表是非空的,因此在逻辑上是True,因此and的真值与第二个列表的真值相同. (在我们的例子中,第二个列表是非空的,因此在逻辑上是True,但是要识别该列表将需要不必要的计算步骤.)

So in Example 1, the first list is non-empty and therefore logically True, so the truth value of the and is the same as that of the second list. (In our case, the second list is non-empty and therefore logically True, but identifying that would require an unnecessary step of calculation.)

例如,列表2不能以位方式有意义地组合,因为它们可以包含任意不同的元素.可以按位组合的内容包括:对与错,整数.

For example 2, lists cannot meaningfully be combined in a bitwise fashion because they can contain arbitrary unlike elements. Things that can be combined bitwise include: Trues and Falses, integers.

NumPy对象支持矢量化计算.也就是说,它们使您可以对多个数据执行相同的操作.

NumPy objects, by contrast, support vectorized calculations. That is, they let you perform the same operations on multiple pieces of data.

示例3失败,因为NumPy数组(长度> 1)没有真值,因为这防止了基于向量的逻辑混乱.

Example 3 fails because NumPy arrays (of length > 1) have no truth value as this prevents vector-based logic confusion.

示例4只是一个向量化的and位操作.

Example 4 is simply a vectorized bit and operation.

底线

  • 如果您不处理数组并且不执行整数的数学运算,则可能需要and.

如果要合并真值向量,请将numpy&结合使用.

If you have vectors of truth values that you wish to combine, use numpy with &.

这篇关于“和"(布尔值)与“&" (按位)-为什么列表与numpy数组在行为上有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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