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

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

问题描述

列表与 NumPy 数组的布尔运算和按位运算的行为有何不同?

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

mylist1 = [真、真、真、假、真]mylist2 = [假,真,假,真,假]>>>len(mylist1) == len(mylist2)真的# ---- 示例 1 ---->>>mylist1 和 mylist2[假,真,假,真,假]# 我会期望 [假,真,假,假,假]# ---- 示例 2 ---->>>mylist1 &我的列表2类型错误:不支持 & 的操作数类型:'list' 和 'list'# 为什么不像示例 1 那样?>>>将 numpy 导入为 np# ---- 示例 3 ---->>>np.array(mylist1) 和 np.array(mylist2)ValueError:包含多个元素的数组的真值不明确.使用 a.any() 或 a.all()# 为什么不像示例 4 那样?# ---- 示例 4 ---->>>np.array(mylist1) &np.array(mylist2)数组([假,真,假,假,假],dtype=bool)# 这是我期待的输出!

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

我阅读了 位运算 以更好地理解这个概念,但我正在努力使用它使我的上述 4 个例子有意义的信息.

示例 4 将我带到了我想要的输出,所以很好,但我仍然对何时/如何/为什么应该使用 and& 感到困惑.为什么列表和 NumPy 数组在使用这些运算符时表现不同?

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

解决方案

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

在 Python 中,空内置对象通常在逻辑上被视为 False,而非空内置对象在逻辑上被视为 True.这有助于常见用例,如果列表为空,您想要做一些事情,如果列表不是,则做其他事情.请注意,这意味着列表 [False] 在逻辑上是 True:

<预><代码>>>>如果 [假]:...打印真"...真的

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

例如 2,列表不能以按位方式进行有意义的组合,因为它们可以包含任意不同的元素.可以按位组合的内容包括:Trues 和 Falses、整数.

相比之下,NumPy 对象支持矢量化计算.也就是说,它们允许您对多条数据执行相同的操作.

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

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

底线

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

  • 如果您有想要组合的真值向量,请将 numpy& 结合使用.

What explains the difference in behavior of boolean and bitwise operations on lists vs NumPy arrays?

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!

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.

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?

Can anyone help me understand the difference between boolean and bitwise operations to explain why they handle lists and NumPy arrays differently?

解决方案

and tests whether both expressions are logically True while & (when used with True/False values) tests if both are 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

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.)

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 objects, by contrast, support vectorized calculations. That is, they let you perform the same operations on multiple pieces of data.

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

Example 4 is simply a vectorized bit and operation.

Bottom Line

  • If you are not dealing with arrays and are not performing math manipulations of integers, you probably want and.

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

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

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