first and last index as in matlab
Rob Williscroft
rtw at freenet.co.uk
Sun Dec 17 13:56:16 EST 2006
Evan wrote in news:1166379931.054933.77450 at j72g2000cwa.googlegroups.com in
comp.lang.python:
> 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?
[ind[0], ind[-1]]
or if you need something that can be generalised:
[ind[i] for i in [0, -1]]
so if you have:
indexes_of_ind = [0, 2, -1, -2]
you can write:
[ind[i] for i in indexes_of_ind]
Rob.
--
http://www.victim-prime.dsl.pipex.com/
More information about the Python-list
mailing list