Efficiently replace elements in array based on dictionary - NumPy / Python

前端 未结 4 1159
梦谈多话
梦谈多话 2020-12-10 05:22

First, of all, my apologies if this has been answered elsewhere. All I could find were questions about replacing elements of a given value, not elements of multiple values.<

4条回答
  •  暖寄归人
    2020-12-10 06:15

    I think the Divakar #3 method assumes that the mapping dict covers all values (or at least the maximum value) in the target array. Otherwise, to avoid index out of range errors, you have to replace the line

    mapping_ar = np.zeros(k.max()+1,dtype=v.dtype) with

    mapping_ar = np.zeros(array.max()+1,dtype=v.dtype)

    That adds considerable overhead.

提交回复
热议问题