1
x = [1,1,1,1]
y = [1,1,1,1]
np.logical_and(x == 1, y == 1)

Output: False

I am expecting an output of [True, True, True, True]. How to achieve this?

1 Answer 1

1

In your example x and y are lists not arrays. When you compare a list with an integer using == you'll always get False:

>>> [1,1,1,1] == 1
False
>>> [1,1,1,1] == 1
False
>>> np.logical_and(False, False)
False

You have to convert them to arrays, because arrays implement == to work element-wise:

>>> x = np.array([1, 1, 1, 1])
>>> x == 1
array([ True,  True,  True,  True])

>>> x = np.array([1, 1, 1, 1])
>>> y = np.array([1, 1, 1, 1])
>>> np.logical_and(x == 1, y == 1)
array([ True,  True,  True,  True])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.