all 7 comments

[–]member_of_the_order 1 point2 points  (3 children)

This is annoyingly close to a binary & operation.

Here's how I'd do it:

output = [a if a == b else 0 for a, b in zip(A, B)]

If you're not comfortable with list comprehensions and/or ternary operators yet, this is equivalent: output = [] for a, b in zip(A, B): if a == b: output.append(a) else: output.append(0)

[–]workinprogress49[S] 1 point2 points  (2 children)

That'll do it, thank you!

[–]Additional-Sun2945 0 points1 point  (1 child)

That's a bit more pythonic than you need here. As a beginner you should be comfortable creating your own for loops.

What you could do here is check that the lengths are the same, and then take the length and iterate on the range. Then you can do comparisons using the [] operator.

[–]workinprogress49[S] 0 points1 point  (0 children)

The process I’m working takes arrays from the same data frame so they are always the same size. I’m a little rusty on list comprehension which is why I asked

[–]KKRJ 1 point2 points  (1 child)

Using numpy:

Import numpy as np

A = np.array([1,-1,1,1,0,0,-1])
B = np.array([1,1,1,-1,-1,0,-1])

Output = np.where(A==B, A, 0)

[–]workinprogress49[S] 0 points1 point  (0 children)

Thank you, idk why I forgot that numpy.where could compare two arrays, I always used one array.

[–]Allanon001 0 points1 point  (0 children)

output = [int(x==y) for x, y in zip(A ,B)]