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

后端 未结 6 835
迷失自我
迷失自我 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

    I found another solution with the numpy function place. (Documentation here)

    Using it on your example:

    >>> a = np.array([1,2,2,1]).reshape(2,2)
    >>> a
    array([[1, 2],
       [2, 1]])
    >>> np.place(a, a==1, 0)
    >>> np.place(a, a==2, 10)
    >>> a
    array([[ 0, 10],
           [10,  0]])
    

提交回复
热议问题