间“和”(布尔型)与差'和;' (按位)的蟒蛇。为什么在名单VS numpy的阵列行为的区别? [英] Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays?

查看:102
本文介绍了间“和”(布尔型)与差'和;' (按位)的蟒蛇。为什么在名单VS numpy的阵列行为的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解释布尔和位操作的行为对名单VS numpy.arrays区别?

我越来越困惑的适当使用的&安培; 'VS''在Python ,在下面的简单的例子说明。

I'm getting confused about the appropriate use of the '&' vs 'and' in python, illustrated in the following simple 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 am confused: I would have expected [False, True, False, False, False]

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

    # ---- Example 3 ----
    >>>import numpy as np

    >>> 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()
    #I am confused: 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 both helped me understand that 'and' is a boolean operation but '&' is a bitwise operation.

一些信息,以更好地理解的位操作的,但我在努力利用这些信息来使我的上述4例感。

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

请注意,在我的特定情况下,我期望的输出是一个newlist其中:

Note, in my particular situation, my desired output is a newlist where:

    len(newlist) == len(mylist1) 
    newlist[i] == (mylist1[i] and mylist2[i]) #for every element of newlist

例4,以上,导致我对我的期望的输出,所以这是很好。

Example 4, above, led me to my desired output, so that is fine.

但我离开感到困惑的时候/如何/为什么我应该使用'和'VS'和;。为什么列表和numpy的阵列,这些运营商不同的表现?

But I am left feeling confused about when/how/why I should use 'and' vs '&'. Why do lists and numpy arrays behave differently with these operators?

谁能帮助我了解和布尔位操作之间的差异来解释为什么他们处理列表和numpy.arrays不同?

我只是想确保我继续使用这些操作正确前进。非常感谢您的帮助!

I just want to make sure I continue to use these operations correctly going forward. Thanks a lot for the help!

Numpy version 1.7.1

python 2.7

References all inline with text.

EDITS

1)由于@delnan为指出,在我原来的例子我有掩盖了更深我上午的混乱模糊。我已经更新我的例子阐明我的问题。

1) Thanks @delnan for pointing out that in my original examples I had am ambiguity that was masking my deeper confusion. I have updated my examples to clarify my question.

推荐答案

同时测试前pressions无论在逻辑上是,而&安培; (用真 / 值)测试都是

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

在Python中,空的内置对象通常被视为逻辑,非空的内置插件是逻辑。这有利于在那里你想要做的事,如果列表为空,别的东西如果列表是不常见的情况。请注意,这意味着名单[虚假]在逻辑上是

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中,第一个列表非空,因此在逻辑上,这样的真值的是相同的,第二列表。 (在本例中,第二个列表非空,因此在逻辑上,但确定这将需要计算的不必要的一步。)

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,列表不能意义地按位时尚相结合,因为它们可以包含不同的元素随心所欲。事情可以合并按位包括:Trues和Falses,整数

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)没有真值,因为这prevents基于矢量的逻辑混乱。

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

例4是一个简单的矢量位操作。

Example 4 is simply a vectorized bit and operation.

底线


  • 如果你不处理阵列和不执行整数运算操作,你可能想

如果您有要合并,使用 numpy的与真值向量&安培;

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

这篇关于间“和”(布尔型)与差'和;' (按位)的蟒蛇。为什么在名单VS numpy的阵列行为的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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