Search code examples
pythonnumpybooleanlogical-operators

logical_not used for 1,0 rather than True, False in boolean array


I want to get the inverse of all components in an array containing a certain number of 0 and 1. When I use numpy.logical_not it returns False and True instead:

import numpy as np
a=np.array([1,0,0])
b=np.logical_not(a)
print b

will return [False,True,True] rather than [0,1,1] (which is what I would like to get). Of course, I can manually create a function that transforms the False back to 0 and the True back to 1, but is there a more direct way to do this ?


Solution

  • 1 - a
    

    Just use arithmetic operators instead of logical operators.