So what's happening here?

Todd toddrjen at gmail.com
Fri Jun 5 09:31:07 EDT 2015


On Fri, Jun 5, 2015 at 3:11 PM, Paul Appleby <pap at nowhere.invalid> wrote:

> On Fri, 05 Jun 2015 14:55:11 +0200, Todd wrote:
>
> > 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:
>
> OK, thanks.  I see.
>
> (I'd have thought that id(a[1]) and id(b[1]) would be the same if they
> were the same element via different "views", but the id's seem to change
> according to rules that I can't fathom.)
>
>
a[1] and b[1] are NOT views.   Slices are views, single elements are not.
They are numpy scalars, which are immutable, so returning a view wouldn't
make sense in such a case since you can't modify it anyway.

Taking a slice creates a new view.  It is looking at the same underlying
data, but it is a new object.  So "a[1:] is b[1:]" is False, as is "a[:] is
b".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150605/0ac854ce/attachment.html>


More information about the Python-list mailing list