first and last index as in matlab

Paul McGuire ptmcg at austin.rr._bogus_.com
Sun Dec 17 14:30:04 EST 2006


"Evan" <evanmason at gmail.com> wrote in message 
news:1166379931.054933.77450 at j72g2000cwa.googlegroups.com...
> In matlab I can do the following:
>
>>> ind = [3,5,7,2,4,7,8,24]
> ind = 3     5     7     2     4     7     8    24
>>> ind(1)      ans =     3
>>> ind(end)     ans =    24
>>> ind([1 end])      ans =      3    24
>
> but I can't get the last line in python:
>
> In [690]: ind = [3,5,7,2,4,7,8,24]
> In [691]: ind[0]    Out[691]: 3
> In [692]: ind[-1:]  Out[692]: [24]
> In [693]:  ??
>
> How do I pull out multiple indices as in matlab?
>
>
> Thanks, Evan
>

Or use the third element of a slice, which defines a stepsize, and pick a 
step that will go from the first to the last element:

>>> lst = list("ABCDEFG")
>>> lst
['A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> lst[0::len(lst)-1]
['A', 'G']

-- Paul





More information about the Python-list mailing list