[Numpy-discussion] Specifying Index Programmatically

Neil Martinsen-Burrell nmb@wartburg....
Sat Aug 8 22:54:35 CDT 2009


On 2009-08-08 22:46 , T J wrote:
> I have an array, and I need to index it like so:
>
>      z[...,x,:]
>
> How can I write code which will index z, as above, when x is not known
> ahead of time. For that matter, the particular dimension I am querying
> is not known either.  In case this is still confusing, I am looking
> for the NumPy way to do:
>
>     z[("...",5,":")]
>
>    z[(":", 3, ":", 5, "...")]
>
>    z[(1, "...", 5)]
>
> Basically, I want to be able to pre-construct what should appear
> inside the [].  The numbers are no problem, but I'm having trouble
> with the ellipsis and colon.

The ellipsis is a built-in python constant called Ellipsis.  The colon 
is a slice object, again a python built-in, called with None as an 
argument.  So, z[...,2,:] == z[Ellipsis,2,slice(None)].

-Neil


More information about the NumPy-Discussion mailing list