Python AND operator on two boolean lists - how?

前端 未结 9 2080
感动是毒
感动是毒 2020-11-28 09:36

I have two boolean lists, e.g.,

x=[True,True,False,False]
y=[True,False,True,False]

I want to AND these lists together, with the expected o

相关标签:
9条回答
  • 2020-11-28 10:12

    This should do what you want:

    xy = [a and b for a, b in zip(x, y)]
    

    The reason x and y returns y and y and x returns x is because boolean operators in python return the last value checked that determines the true-ness of the expression. Non-empty list's evaluate to True, and since and requires both operands to evaluate True, the last operand checked is the second operand. Contrast with x or y, which would return x because it doesn't need to check y to determine the true-ness of the expression.

    0 讨论(0)
  • 2020-11-28 10:19

    and is not necessarily a Boolean operator; it returns one of its two arguments, regardless of their type. If the first argument is false-ish (False, numeric zero, or an empty string/container), it returns that argument. Otherwise, it returns the second argument.

    In your case, both x and y are non-empty lists, so the first argument is always true-ish, meaning x and y returns y and y and x returns x.

    0 讨论(0)
  • 2020-11-28 10:22

    You could use numpy:

    >>> import numpy as np
    >>> x=np.array([True,True,False,False])
    >>> y=np.array([True,False,True,False])
    >>> x & y
    array([ True, False, False, False], dtype=bool)
    

    Numpy allows numerical and logical operations on arrays such as:

    >>> z=np.array([1,2,3,4])
    >>> z+1
    array([2, 3, 4, 5])
    

    You can perform bitwise and with the & operator.

    Instead of a list comprehension, you can use numpy to generate the boolean array directly like so:

    >>> np.random.random(10)>.5
    array([ True,  True,  True, False, False,  True,  True, False, False, False], dtype=bool)
    
    0 讨论(0)
  • 2020-11-28 10:23

    Here is a simple solution:

    np.logical_and(x,y)
    
    0 讨论(0)
  • 2020-11-28 10:27

    and simply returns either the first or the second operand, based on their truth value. If the first operand is considered false, it is returned, otherwise the other operand is returned.

    Lists are considered true when not empty, so both lists are considered true. Their contents don't play a role here.

    Because both lists are not empty, x and y simply returns the second list object; only if x was empty would it be returned instead:

    >>> [True, False] and ['foo', 'bar']
    ['foo', 'bar']
    >>> [] and ['foo', 'bar']
    []
    

    See the Truth value testing section in the Python documentation:

    Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

    [...]

    • any empty sequence, for example, '', (), [].

    [...]

    All other values are considered true — so objects of many types are always true.

    (emphasis mine), and the Boolean operations section right below that:

    x and y
    if x is false, then x, else y

    This is a short-circuit operator, so it only evaluates the second argument if the first one is True.

    You indeed need to test the values contained in the lists explicitly. You can do so with a list comprehension, as you discovered. You can rewrite it with the zip() function to pair up the values:

    [a and b for a, b in zip(x, y)]
    
    0 讨论(0)
  • 2020-11-28 10:27

    You can use the zip function

    x=[True,True,False,False]
    y=[True,False,True,False]
    z=[a and b for a,b in zip(x,y)]
    
    0 讨论(0)
提交回复
热议问题