Supposing A is an NP array.
If I do this:
B = np.copy(A)
B[B !=0] = 1
or
A[A != 0]=1
B=np.copy(A)
I get B as the masked version of A, i.e. the desired output. However if I try the assignment like this:
B= A[A !=0]=1
B becomes an integer for reasons I don't understand. Why does this happen and is there a way or performance reason to perform this operation as a single assignment?
I must preface this by saying any attempt in doing so massively decreases readability. Just use two lines if you don't want to other people (or yourself) to ever work with that code again. This answer is just supposed to demonstrate what could be done, not what should be done.
The expression A=B=x
assigns x
to both A
and B
. If you really want to squeeze everything onto one line you could try something like
import numpy as np
a = np.arange(5)
(b:=a.copy())[a!=0]=1
The :=
(walrus) operator actually evaluates to the assigned value, unlike the assignment (=
) operator. (Note that A=B=x
works because it is basically a shorthand for t=x; A=t; B=t
, but A=(B=x)
will not work as the assignment does not evaluate to anything. You chould write A=(B:=x)
though.) Then a
remains unchanged, which corresponds to your first version, so
>>> b
array([0, 1, 1, 1, 1])
>>> a
array([0, 1, 2, 3, 4])