[Numpy-discussion] copying array to itself

Stéfan van der Walt stefan at sun.ac.za
Thu May 3 16:03:55 EDT 2012

On Thu, May 3, 2012 at 1:51 AM, Henry Gomersall <heng at cantab.net> wrote:
> Right, so this is expected behaviour then. Is this documented somewhere?
> It strikes me that this is pretty unexpected behaviour.

Imagine the way you would code this in a for-loop.  You want

a = np.arange(10)
a[2:] = a[:-2]

Now you write:

for i in range(2, len(a)):
    a[i] = a[i - 2]

which yields

[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

One of the great things about NumPy is that it only copies data when
it really has to, and in this case it would need to be very clever to
figure out what you are trying to do.


More information about the NumPy-Discussion mailing list