first and last index as in matlab
Robert Kern
robert.kern at gmail.com
Sun Dec 17 17:42:48 EST 2006
Beliavsky wrote:
> Evan wrote:
>> 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?
>
> If you want functionality similar to Matlab in Python, you should use
> Numpy, which has the "take" function to do what you want.
Actually, in numpy, we also have "fancy indexing" similar to Matlab's:
In [1]: from numpy import *
In [2]: ind = array([3,5,7,2,4,7,8,24])
In [3]: ind[[0, -1]]
Out[3]: array([ 3, 24])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list