[Tutor] about array

Alan Gauld alan.gauld at btinternet.com
Tue Dec 26 12:51:22 CET 2006


"linda.s" <samrobertsmith at gmail.com> wrote 

>>>> a
> array([[ 0., 1., 2., 3., 4., 5.],
> [ 6., 7., 8., 9., 10., 11.],
> [ 12., 13., 14., 15., 16., 17.],
> [ 18., 19., 20., 21., 22., 23.],
> [ 24., 25., 26., 27., 28., 29.]])

OK, This sets up your test array.

>>>> a[1:3,:-1:2]                 # a[i,j] for i=1,2 and j=0,2,4

And this passes two slices.
The first is 1:3 which means 1,2 - normal slice behaviour
he second is :-1:2 which uses extended slice syntax to 
specify a stepsize. So the slice says go from 0 (default) 
to the last element(-1) using a step sizeof 2, which is 0,2,4

So we extract the 0,2,4 elements from rows 1,2 to get:

> array([[ 6., 8., 10.],
> [ 12., 14., 16.]])

>>>> a[::3,2:-1:2]                     # a[i,j] for i=0,3 and j=2,4

Similarly the first slice here is the whole array(:) with a 
step size of 3, thus 0,3
The second slice is 2:-1:2 which means in practice start 
at 2 and go to the end stepping in 2s, which is: 2,4
So this time we take the 2,4 index items from rows 0,3
which is:

> array([[ 2., 4.],
> [ 20., 22.]])

Its just normal slicing notation but with a pair of them 
inside the brackets instead of one.

Which module are you using that supports this? 
I've never seen it before.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list