簡體   English   中英

將一個數組的值替換為另一個數組的索引時,用另一個數組的值替換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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM