My impression is that in NumPy, two arrays can share the same memory. Take the following example:
import numpy as np
a=np.arange(27)
b=a.reshape((3,3,3))
a[0]=5000
print (b[0,0,0]) #5000
#Some tests:
a.data is b.data #False
a.data == b.data #True
c=np.arange(27)
c[0]=5000
a.data == c.data #True ( Same data, not same memory storage ), False positive
So clearly b
didn't make a copy of a
; it just created some new meta-data and attached it to the same memory buffer that a
is using. Is there a way to check if two arrays reference the same memory buffer?
My first impression was to use a.data is b.data
, but that returns false. I can do a.data == b.data
which returns True, but I don't think that checks to make sure a
and b
share the same memory buffer, only that the block of memory referenced by a
and the one referenced by b
have the same bytes.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…