Numpy answer:
Arrays in numpy are views/indexes on a backing storage.
You can copy the view, without copying the backing storage...
a=numpy.array([1,2,3,4])
b=a[:] # copy of the array ("view" or "index"), not the storage
b.shape=(2,2)
print a
# [1 2 3 4]
print b
# [[1 2]
# [3 4]]
b *= 2
print a
# [2 4 6 8]
print b
# [[2 4]
# [6 8]]
See how changing b affected a? Yet they still have a different shape. Consider them to be views of the data; and the b=a[:]
line copied just this view. I could even modify the shape of b
. Because it is just an index to the data, that says where columns and rows are located in memory.
If you want a copy of the backing storage in numpy, use a.copy()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…