Replace values of a numpy index array with values of a list

后端 未结 6 825
迷失自我
迷失自我 2020-12-15 04:31

Suppose you have a numpy array and a list:

>>> a = np.array([1,2,2,1]).reshape(2,2)
>>> a
array([[1, 2],
       [2, 1]])
>>> b = [         


        
6条回答
  •  眼角桃花
    2020-12-15 05:21

    Read-only array in numpy can be made writable:

    nArray.flags.writeable = True
    

    This will then allow assignment operations like this one:

    nArray[nArray == 10] = 9999 # replace all 10's with 9999's
    

    The real problem was not assignment itself but the writable flag.

提交回复
热议问题