[Numpy-discussion] inconsistent semantics for double-slicing

Alex Flint alex.flint at gmail.com
Wed Jul 27 17:36:51 EDT 2011


When applying two different slicing operations in succession (e.g. select a
sub-range, then select using a binary mask) it seems that numpy arrays can
be inconsistent with respect to assignment:

For example, in this case an array is modified:
In [6]: *A = np.arange(5)*
In [8]: *A[:][A>2] = 0*
In [10]: A
Out[10]: *array([0, 1, 2, 0, 0])*

Whereas here the original array remains unchanged
In [11]: *A = np.arange(5)*
In [12]: *A[[0,1,2,3,4]][A>2] = 0*
In [13]: A
Out[13]: *array([0, 1, 2, 3, 4])*

This arose in a less contrived situation in which I was trying to copy a
small image into a large image, modulo a mask on the small image.

Is this meant to be like this?

Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110727/a8bbbefb/attachment.html>


More information about the NumPy-Discussion mailing list