[Numpy-discussion] Views and Increments

Anakim Border akborder at gmail.com
Mon Aug 8 09:11:17 EDT 2016


Dear List,

I'm experimenting with views and array indexing. I have written two code
blocks that I was expecting to produce the same result.

First try:

>>> a = np.arange(10)
>>> b = a[np.array([1,6,5])]
>>> b += 1
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])


Alternative version:

>>> a = np.arange(10)
>>> a[np.array([1,6,5])] += 1
>>> a
array([0, 2, 2, 3, 4, 6, 7, 7, 8, 9])


I understand what is happening in the first case. In fact, the
documentation is quite clear on the subject:

For all cases of index arrays, what is returned is a copy of the original
data, not a view as one gets for slices.

What about the second case? There, I'm not keeping a reference to the
intermediate copy (b, in the first example). Still, I don't see why the
update (to the copy) is propagating to the original array. Is there any
implementation detail that I'm missing?

Best,
  ab
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160808/51dd5fc3/attachment.html>


More information about the NumPy-Discussion mailing list