我想更改与index = x[index]
numpy数组x = [1,2,3,4,0,1,2,3]
另一个Numpy数组y = [3,4,0,1,2]
for i in range(len(x)):
x[i] = y[x[i]]
有没有更快的方法呢?
答案 0 :(得分:0)
使用:
x = y[x]
示例:
>>> import numpy as np
>>> x = np.array( [1,2,3,4,0,1,2,3] )
>>> y = np.array( [3,4,0,1,2] )
>>> y[x]
array([4, 0, 1, 2, 3, 4, 0, 1])
或者,为了证明作业:
>>> x = y[x]
>>> x
array([4, 0, 1, 2, 3, 4, 0, 1])