Skip to main content
 首页 » 编程设计

python之将值添加到 numpy 数组中的部分

2025年05月04日14telwanggs

我在 numpy 中有一个一维数组,我想将某个值添加到数组的一部分。

例如,如果数组是:

a = [1, 2, 3, 4, 5] 

我想将值 7 添加到第 2 和第 3 列以获得:

a = [1, 2, 10, 11, 5] 

有什么简单的方法可以做到这一点吗?

谢谢!

请您参考如下方法:

您可以 index the array with another array包含索引:

a[[2,3]] += 7