temp in your example is list , which is clearly not equal to 1. Thus, the expression
temp[temp != 1] = 0
actually
temp[True] = 0
Convert temp to a NumPy array to get the broadcast behavior you need.
>>> import numpy as np >>> temp = np.array([1,2,3,4,5,6]) >>> temp[temp != 1] = 0 >>> temp array([1, 0, 0, 0, 0, 0])
source share