Find and replace multiple values in python

前端 未结 6 1201
攒了一身酷
攒了一身酷 2021-02-15 03:59

I want to find and replace multiple values in an 1D array / list with new ones.

In example for a list

a=[2, 3, 2, 5, 4, 4, 1, 2]

I woul

6条回答
  •  一整个雨季
    2021-02-15 04:55

    >>> arr = np.empty(a.max() + 1, dtype=val_new.dtype)
    >>> arr[val_old] = val_new
    >>> arr[a]
    array([3, 4, 3, 1, 5, 5, 2, 3])
    

提交回复
热议问题