[Numpy-discussion] Determining if two arrays share data

Stefan van der Walt stefan at sun.ac.za
Thu Jul 6 02:21:18 CDT 2006


On Thu, Jul 06, 2006 at 11:39:19AM +0900, Bill Baxter wrote:
> Often when I'm doing interactive prototyping I find myself wanting to check
> whether two arrays are sharing a copy of the same data.
> 
> It seems like there ought to be a concise way to do that, but right now seems
> like the only way is with a little function like this:
> 
> def same_base(a,b):
>     ab = a.base
>     if ab is None: ab = a
>     bb = b.base
>     if bb is None: bb = b
>     return ab is bb   
> 
> is there some easier built-in way?  Does the above function even cover all the
> bases? (so to speak...)

Say you have

x = N.array([1,2,3,4])

and

y = x.reshape((2,2))

then x and y share the same data.  You can see this when you do

x.__array_interface__['data'][0] == y.__array_interface__['data'][0]

Still, this only holds for full data views.  If you had

z = y[1:,1:]

then the data memory position would differ.

Cheers
Stéfan




More information about the Numpy-discussion mailing list