![](/img/trans.png)
[英]Replacing values in one array with corresponding values in another array, using Numpy
[英]replacing the values in a numpy array with the values of another array against value of one array as index in another
我想更改index = x[index]
numpy array(y)中的值所對應的numpy array(x)中的值
一個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]]
有沒有更快的方法可以做到這一點?
采用:
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])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.