Reputation: 1874
I have two python lists
A
and B
of equal length each containing only boolean values. Is it possible to get a third list C
where C[i] = A[i] and B[i]
for 0 <= i < len(A)
without using loop?
I tried following
C = A and B
but probably it gives the list B
I also tried
C = A or B
which gives first list
I know it can easily be done using for loop in single line like C = [x and y for x, y in zip(A, B)]
.
Upvotes: 0
Views: 1363
Reputation: 28606
Is it possible to get a third list
C
whereC[i] = A[i] and B[i]
for0 <= i < len(A)
without using loop?
Kind of:
class AndList(list):
def __init__(self, A, B):
self.A = A
self.B = B
def __getitem__(self, index):
return self.A[index] and self.B[index]
A = [False, False, True, True]
B = [False, True, False, True]
C = AndList(A, B)
print isinstance(C, list) and all(C[i] == (A[i] and B[i])
for i in range(len(A)))
Prints True
.
Upvotes: 0
Reputation: 21615
Simple answer: you can't.
Except in the trivial way, which is by calling a function that does this for you, using a loop. If you want this kind of nice syntax you can use libraries as suggested: map, numpy, etc. Or you can write your own function.
If what you are looking for is syntactic convenience, Python does not allow overloading operators for built-in types such as list.
Oh, and you can use recursion, if that's "not a loop" for you.
Upvotes: 0
Reputation: 55479
You can do it without an explicit loop by using map
, which performs the loop internally, at C speed. Of course, the actual and
operation is still happening at Python speed, so I don't think it'll save much time (compared to doing essentially the same thing with Numpy, which can not only do the looping at C speed, it can do the and operation at C speed too. Of course, there's also the overhead of converting between native Python lists & Numpy arrays).
Demo:
from operator import and_
a = [0, 1, 0, 1]
b = [0, 0, 1, 1]
c = map(and_, a, b)
print c
output
[0, 0, 0, 1]
Note that the and_
function performs a bitwise and operation, but that should be ok since you're operating on boolean values.
Upvotes: 2
Reputation: 9863
I'd recommend you use numpy to use these kind of predicates over arrays. Now, I don't think you can avoid loops to achieve what you want, but... if you don't consider mapping or enumerating as a form of looping, you could do something like this (C1):
A = [True, True, True, True]
B = [False, False, True, True]
C = [x and y for x, y in zip(A, B)]
C1 = map(lambda (i,x): B[i] and x, enumerate(A))
C2 = [B[i] and x for i,x in enumerate(A)]
print C==C1==C2
Upvotes: 1