Why does an assignment for double-sliced numpy arrays not work?

前端 未结 4 2546
甜味超标
甜味超标 2021-02-20 05:18

why do the following lines not work as I expect?

import numpy as np
a = np.array([0,1,2,1,1])
a[a==1][1:] = 3
print a
>>> [0 1 2 1 1]
# I would expect [         


        
4条回答
  •  -上瘾入骨i
    2021-02-20 05:32

    Because the a[a==1] part isn't actually a slice. It creates a new array. It makes sense when you think about it-- you're only taking the elements that satisfy the boolean condition (like a filter operation).

提交回复
热议问题