我有源和目标 numpy 数组,比如说
dest = np.arange(1000)
src = np.random(500)
我想将 src 复制到 dest 数组,覆盖前 50 个数字,然后覆盖从索引 100 到 150 的所有数字,然后从 200 到 250,依此类推,直到 900 到 950。
为了计算指数,我使用以下方法:
index = np.reshape([100*i+np.arange(50) for i in range(10)],-1)
然后我就用
dest[index]=src
要复制剩余元素(使用另一个 src,比如 src2),我只需调整索引
index2 = np.reshape([50+100*i+np.arange(50) for i in range(10)],-1)
dest[index2]=src2
我很确定有一种更优雅/更有效的方法来实现此目的,而无需显式构建索引。
有更好的方法来执行复制吗?
最佳答案
dest.reshape(-1,50)[::2] = src.reshape(-1,50)
对于src2
:
dest.reshape(-1,50)[1::2] = src2.reshape(-1,50)
关于python - 批处理 numpy 数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41259770/