[Numpy-discussion] bug with assignment into an indexed array?
Benjamin Root
ben.root@ou....
Thu Aug 11 10:16:37 CDT 2011
On Thu, Aug 11, 2011 at 8:37 AM, Olivier Delalleau <shish@keba.be> wrote:
> Maybe confusing, but working as expected.
>
>
> When you write:
> matched_to[np.array([0, 1, 2])] = 3
> it calls __setitem__ on matched_to, with arguments (np.array([0, 1, 2]),
> 3). So numpy understand you want to write 3 at these indices.
>
>
> When you write:
> matched_to[:3][match] = 3
> it first calls __getitem__ with the slice as argument, which returns a view
> of your array, then it calls __setitem__ on this view, and it fills your
> matched_to array at the same time.
>
>
> But when you write:
> matched_to[np.array([0, 1, 2])][match] = 3
> it first calls __getitem__ with the array as argument, which retunrs a
> *copy* of your array, so that calling __setitem__ on this copy has no effect
> on your original array.
>
> -=- Olivier
>
>
Right, but I guess my question is does it *have* to be that way? I guess it
makes some sense with respect to indexing with a numpy array like I did with
the last example, because an element could be referred to multiple times
(which explains the common surprise with '+='), but with boolean indexing,
we are guaranteed that each element of the view will appear at most once.
Therefore, shouldn't boolean indexing always return a view, not a copy? Is
the general case of arbitrary array selection inherently impossible to
encode in a view versus a slice with a regular spacing?
Thanks,
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110811/f10a96e5/attachment.html
More information about the NumPy-Discussion
mailing list