简体   繁体   English

如何用另一个带索引的数组替换一个 numpy 数组中的值

[英]How replace values from one numpy array by other array with indices

I have two arrays and one list with indices我有两个 arrays 和一个带索引的列表

Array source (array 01):阵列源(阵列01):

x = np.array([[1,2,3], [4,5,6], [7,8,9]])
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Array with new values (array 02):具有新值的数组(数组 02):

y = np.array([11, 22, 33])
array([11, 22, 33])

and a list with indices和一个索引列表

a = [1,2,1]
[1, 2, 1]

I need to replace the values of array 02 in array 01 based on indices我需要根据索引替换数组 01 中数组 02 的值

The return expected:预期回报:

array([[1, 11, 3],
       [4, 5, 22],
       [7, 33, 9]])

How to do this without loop?如何在没有循环的情况下做到这一点?

Thanks in advance.提前致谢。

Tryadvanced indexing :尝试高级索引

x[np.arange(len(x)), a] = y

I found also this tutorial easier for me than official documentation.我还发现本教程对我来说比官方文档更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM