Problem introduction
When performing Boolean operations on a Boolean array, and reports an error, and & is normal. After careful study, it is found that there is no difference between and or and & | in python.
In [37]: a=np.arange(-3,3)
Out[37]: array([-3, -2, -1, 0, 1, 2])
In [38]: a>0
Out[38]: array([False, False, False, False, True, True])
In [39]: a<2
Out[39]: array([ True, True, True, True, True, False])
In [40]: a>0 and a<2
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-40-1d57017acc9c> in <module>
----> 1 a>0 and a<2
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
In [41]: (a>0) & (a<2)
Out[41]: array([False, False, False, False, True, False])
and or is a logical operator
And, or, not are logical (Boolean) operators, and perform NOR operation on logical (Boolean) variables. But note: when the variable is a numeric variable, it will also be calculated according to a certain rule.
as follows:
Operator | expression | Calculation rules | Examples |
---|---|---|---|
and | a and b |
a and b are logical variables: when a and b are both True, return True; a and b are numeric variables: return b |
True and False Return False 2 and 1 Return 1 |
or | a or b |
a and b are logical variables: when a and b have a True, return True; a and b are numeric variables: when a is non-zero, return a, otherwise return b |
True or False Return True 2 and 1 Return 2 |
not | not a |
a is a logical variable: non-operation; a is a numeric variable: when a is non-zero, returns False |
not True Return False not 2 return False |
Note: and or cannot perform logical operations on Boolean arrays, that is, those used in the introduction of the problem a>0 and a<2
will report errors. Reason: True and False may exist in the Boolean array, so it is impossible to judge the purpose of the operation, so a ValueError exception is thrown, and prompts us to use a.any () or a.all () to first change the Boolean array into a Boolean value.
& | Is a bit operator
The so-called bit operator is to convert an integer (int) into a binary and then calculate it bit by bit. It is summarized as follows, where a and b are int values, or Boolean values (True, False), and can also be int arrays or Boolean arrays. If it is an array, then bit operations will be performed on the corresponding indexed elements.
Operator | expression | Calculation rules | Examples |
---|---|---|---|
& | a & b |
Bitwise and | 1 & 0 Returning 0 is 1 & 3 equivalent to 01 & 11 returning 1 |
| | a | b |
Bitwise or | 1 | 0 Returning 1 is 1 & 3 equivalent to 01 | 11 returning 3 (binary is 11) |
When a and b are Boolean values (True, False), namely (1, 0), the effect of the & and | bit operators at this time is equivalent to the logical operator. And & and | support operations on arrays, so you can use & and | to perform logical operations on Boolean arrays. So the above (a>0) & (a<2)
is correct. But be careful: because the precedence of operator & is higher than that of comparison operator>, <, you need to use parentheses.
logical_and, logical_or
The logical operators and, or, not in Python cannot be overloaded, so they cannot support logical operations on arrays. However, the numpy module provides standard functions to implement logical operations on arrays. which isnp.logical_and, np.logical_or, np.logical_not
In [30]: np.logical_and(a>0,a<2)
Out[30]: array([False, False, False, False, True, False])
to sum up
- and and or are logical operators, but logical operations cannot be performed on Boolean arrays;
- & And | are bit operators, which can perform bit operations on Boolean arrays, but the results of their operations are equivalent to logical operations
- numpy provides logical_and, logical_or functions to perform logical operations on Boolean arrays.