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

Timothy Hochberg tim.hochberg@ieee....
Mon Oct 29 08:36:32 CDT 2007

On 10/28/07, Matthieu Brucher <matthieu.brucher@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

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.


.  __
.   |-\
.  tim.hochberg@ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20071029/a546ce33/attachment-0001.html 

More information about the Numpy-discussion mailing list