Change the values of a NumPy array that are NOT in a list of indices

I don’t know of a clean way to do something like this:

mask = np.ones(a.shape,dtype=bool) #np.ones_like(a,dtype=bool)
mask[indices] = False
a[~mask] = 999
a[mask] = 888

Of course, if you prefer to use the numpy data-type, you could use dtype=np.bool_ — There won’t be any difference in the output. it’s just a matter of preference really.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)