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 output:
xy=[True,False,False,False]
I thought that expression x and y
would work, but came to discover that it does not: in fact, (x and y) != (y and x)
Output of x and y
: [True,False,True,False]
Output of y and x
: [True,True,False,False]
Using list comprehension does have correct output. Whew!
xy = [x[i] and y[i] for i in range(len(x)]
Mind you I could not find any reference that told me the AND operator would work as I tried with x and y. But it's easy to try things in Python. Can someone explain to me what is happening with x and y
?
And here is a simple test program:
import random random.seed() n = 10 x = [random.random() > 0.5 for i in range(n)] y = [random.random() > 0.5 for i in range(n)] # Next two methods look sensible, but do not work a = x and y z = y and x # Next: apparently only the list comprehension method is correct xy = [x[i] and y[i] for i in range(n)] print 'x : %s'%str(x) print 'y : %s'%str(y) print 'x and y : %s'%str(a) print 'y and x : %s'%str(z) print '[x and y]: %s'%str(xy)
Using Python's and Operator With Boolean Expressions You'll typically use logical operators to build compound Boolean expressions, which are combinations of variables and values that produce a Boolean value as a result. In other words, Boolean expressions return True or False .
You can have multiple Boolean Operators in one statement. It doesn't matter that there are multiple statements. Each statement must be true in order for the whole to evaluate to True .
We combine Boolean values using four main logical operators (or logical connectives): not, and, or, and ==. All decisions that can be made by Python—or any computer language, for that matter—can be made using these logical operators.
and is a Logical AND that returns True if both the operands are true whereas '&' is a bitwise operator in Python that acts on bits and performs bit by bit operation. Note: When an integer value is 0, it is considered as False otherwise True when using logically.
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
orwhile
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 yThis 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)]
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With