Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

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

I was wondering whether there is a way to refer data from many different arrays to one array, but without copying it.

Example:

import numpy as np
a = np.array([2,3,4,5,6])
b = np.array([5,6,7,8])

c = np.ndarray([len(a)+len(b)])

offset = 0
c[offset:offset+len(a)] = a
offset += len(a)
c[offset:offset+len(b)] = b

However, in the example above, c is a new array, so that if you modify some element of a or b, it is not modified in c at all.

I would like that each index of c (i.e. c[0], c[1], etc.) refer to each element of both a and b, but like a pointer, without making a deepcopy of the data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...