-5

I have two lists in python

list1=[1,3,5,-3,-3]
list2=[2,-3,3,-3,5]

I want to produce a list that is true when the elements are of the same sign

result=[True,False,True,True,False]

which is the fastest and more pythonic way to do this?

EDIT:

Solution

I solve it by transforming both list to numpy arrays

np.array(list1)*np.array(list1)>0
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

1 Answers1

2

The product of two numbers of the same sign should be positive, so you can do:

result = [a * b > 0 for a, b in zip(list1, list2)]
blhsing
  • 91,368
  • 6
  • 71
  • 106