How to convert ‘false’ to 0 and ‘true’ to 1 in Python
Use int()
on a boolean test:
x = int(x == 'true')
int()
turns the boolean into 1
or 0
. Note that any value not equal to 'true'
will result in 0
being returned.
If B
is a Boolean array, write
B = B*1
(A bit code golfy.)
You can use x.astype('uint8')
where x
is your Boolean array.