[Numpy-discussion] Getting an item in an array with its coordinates given by another array

Timothy Hochberg tim.hochberg at ieee.org
Mon Oct 29 09:36:32 EDT 2007


On 10/28/07, Matthieu Brucher <matthieu.brucher at gmail.com> wrote:
>
> > Little correction, only c[(2,3)] gives me what I expect, not c[[2,3]],
> > which
> > > is even stranger.
> >
> > c[(2,3)] is the same as c[2,3] and obviously works as you expected.
>
>
>
> Well, this is not indicated in the documentation.
>

This is true at the Python level and is not related to numpy. "x=c[2,3]" is
equivalent to "x=c.__getitem__((2,3))".  Note that the index pair is passed
as a tuple. On the other hand, a single index is not passed as a tuple, but
is instead passed as is. For example: "x = c[a]" gets passed as
"x=c.__getitem__(a)". If 'a' happens to be '(2,3)' you get the behavior
above.

So, although lists and arrays can be used for "fancy-indexing", tuples
cannot be since you can't tell the difference between a tuple of indices and
multiple indices inside square brackets.


[SNIP]





-- 
.  __
.   |-\
.
.  tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20071029/a546ce33/attachment.html>


More information about the NumPy-Discussion mailing list