-1

I was busy with combining masks and it strikes me that:

>>> [False, True] and [True, False]
[True, False]

and

>>> [True, False] and [False, True]
[False, True]

Why is this the case? Why shouldn't I expect it to be [False, False] in both cases?

I also think this is why np.logical_and() exists in Numpy:

>> np.logical_and([True, False], [False, False])
array([False, False])
Olaf
  • 587
  • 4
  • 6

1 Answers1

2

This is the same reason why:

>>> (1,2) and (3,4)
(3, 4)

You need to understand that this is not doing element-wise comparison as opposed to np.logical_and.

The way and works is, if you have a and b, it checks whether a is False, if yes, return a else return b, does not matter what the value of b is.

In your case [False, True] is not False:

>>> bool([False, True])
True

Because it is a non-empty list, even [False, False] is True. So in the case of [False, True] and [True, False], it checks to see whether [False, True] is False, which it is not, so it returns the second value. Same for the other case.

A python implementation of the and or logic would be:

def AND(first, second):
    if bool(first) == True:
        return second
    else:
        return first


def OR(first, second):
    if bool(first) == True:
        return first
    else:
        return second
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52