[Numpy-discussion] index the last several members of a ndarray

Chao YUE chaoyuejoy@gmail....
Tue Oct 18 09:14:03 CDT 2011


thanks Scott. very good explanation.

cheers,

Chao

2011/10/18 Scott Sinclair <scott.sinclair.za@gmail.com>

> On 18 October 2011 13:56, Chao YUE <chaoyuejoy@gmail.com> wrote:
> > but it's strange that if you use b[...,-1],
> > you get:
> > In [402]: b[...,-1]
> > Out[402]: array([ 9, 19])
> >
> > if use b[...,-4:-1],
> > you get:
> > Out[403]:
> > array([[ 6,  7,  8],
> >        [16, 17, 18]])
>
> That's because you're mixing two different indexing constructs. In the
> first case, you're using direct indexing, so you get the values in b
> at the index you specify.
>
> In the second example you're using slicing syntax, where you get the
> values in b at the range of indices starting with -4 and ending *one
> before* -1 i.e. the values at b[..., -2].
>
> Here's a simpler example:
>
> In [1]: a = range(5)
>
> In [2]: a
> Out[2]: [0, 1, 2, 3, 4]
>
> In [3]: a[0]
> Out[3]: 0
>
> In [4]: a[2]
> Out[4]: 2
>
> In [5]: a[0:2]
> Out[5]: [0, 1]
>
> In [6]: a[-3]
> Out[6]: 2
>
> In [7]: a[-1]
> Out[7]: 4
>
> In [8]: a[-3:-1]
> Out[8]: [2, 3]
>
> Cheers,
> Scott
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
***********************************************************************************
Chao YUE
Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex
Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111018/ba8295fd/attachment-0001.html 


More information about the NumPy-Discussion mailing list