Is there any way to make a soft reference or Pointer-like objects using Numpy arrays?

人走茶凉 提交于 2019-11-28 13:46:20

As @Jaime says, you can't generate a new array whose contents point to elements in multiple existing arrays, but you can do the opposite:

import numpy as np

c = np.arange(2, 9)
a = c[:5]
b = c[3:]
print(a, b, c)
# (array([2, 3, 4, 5, 6]), array([5, 6, 7, 8]), array([2, 3, 4, 5, 6, 7, 8]))

b[0] = -1

print(c,)
# (array([ 2,  3,  4, -1,  6,  7,  8]),)

I think the fundamental problem with what you're asking for is that numpy arrays must be backed by a continuous block of memory that can be regularly strided in order to map memory addresses to the individual array elements.

In your example, a and b will be allocated within non-adjacent blocks of memory, so there will be no way to address their elements using a single set of strides.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!