且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改不在索引列表中的 NumPy 数组的值

更新时间:2022-11-14 09:10:20

我不知道有什么干净的方法来做这样的事情:

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

当然,如果您更喜欢使用 numpy 数据类型,则可以使用 dtype=np.bool_ -- 输出不会有任何区别.这真的只是一个偏好问题.

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.