You can use list comprehension
l3 = [(i and j) for i,j in zip(l1,l2)]
l4 = [(i or j) for i,j in zip(l1,l2)]
When you are doing
l3 = l1 and l2
l4 = l1 or l2
then it is getting reduced to
l3 = bool(l1) and l2
l4 = bool(l1) or l2
Now since l1
is non-empty list, so
bool(l1) = True
Now, what I assume in the case of l3 = l1 and l2
, l1
is evaluated as True
so for short circuit evaluation
, it returns l2
.
In case of l4 = l1 or l2
, again due to short circuit evaluation
, l1
is returned since l1
is True
.
So, you are getting the result like that.
short circuit evaluation
is nothing but--
- In the case of
A and B
, if A evaluates to True go on evaluating B and return the result of B. If A evaluates to False, then no point of evaluating B. Return the result of A.
False and print('hello')
>>> False
True and print('hello')
>>> hello
- In the case of
A or B
, if A evaluates to True, then there is no point in evaluating B. Just return the result of A. If A evaluates to False, then return the result of B.
True or (1/0)
>>> True
False or (1/0)
>>> ZeroDivisionError: division by zero
Note:
bool([]) = False
bool([1,2,'a']) = True
bool(['False']) = True
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…