<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Jun 5, 2015 at 2:46 PM, Paul Appleby <span dir="ltr"><<a href="mailto:pap@nowhere.invalid" target="_blank">pap@nowhere.invalid</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I saw somewhere on the net that you can copy a list with slicing. So<br>
what's happening when I try it with a numpy array?<br>
<br>
>>> a = numpy.array([1,2,3])<br>
>>> b = a[:]<br>
>>> a is b<br>
False<br>
>>> b[1] = 9<br>
>>> a<br>
array([1, 9, 3])<br>
<span class=""><font color="#888888"><br></font></span></blockquote><div><br>Numpy arrays are not lists, they are numpy arrays. They are two different data types with different behaviors.  In lists, slicing is a copy.  In numpy arrays, it is a view (a data structure representing some part of another data structure).  You need to explicitly copy the numpy array using the "copy" method to get a copy rather than a view:<br><br>>>> a = numpy.array([1,2,3])<br>
>>> b = a.copy()<br>
>>> a is b<br>
False<br>
>>> b[1] = 9<br>
>>> a<br>
array([1, 2, 3])<br></div></div><br></div><div class="gmail_extra">Here is how it works with lists:<br><br>>>> a = [1,2,3]<br>
>>> b = a[:]<br>
>>> a is b<br>
False<br>
>>> b[1] = 9<br>
>>> a<br>[1, 2, 3]<br></div></div>