P: n/a

I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of
them how do I select them out. In MATLAB, if I just want the first,
fifth and eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
Thanks,
Eric  
Share this Question
P: n/a

On Tue, 11 Nov 2008 11:47:53 0800, Eric wrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of them
how do I select them out. In MATLAB, if I just want the first, fifth and
eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
b = [a[i] for i in [1, 5, 8]]
Ciao,
Marc 'BlackJack' Rintsch  
P: n/a

On Tue, Nov 11, 2008 at 5:47 PM, Eric <er********@gmail.comwrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of
them how do I select them out. In MATLAB, if I just want the first,
fifth and eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
MATLAB works with 1based index, while Python is 0based, so accessing
the index number 8 wouldn't be valid with 8 elements here in Python.
Now, to solve your problem you could either use the already suggested
answer above mine or depending on what you what else you wanna do, you
will feel more comfortable using numpy.
Supposing you have a array with 8 elements: x = numpy.arange(8)
To pull out the first, fifth and eighth elements you would do:
x[numpy.array([0, 4, 7])]
It is so no natural as it was in MATLAB, but well, it was a different language.
Thanks,
Eric
 http://mail.python.org/mailman/listinfo/pythonlist

 Guilherme H. Polo Goncalves  
P: n/a

On Nov 11, 1:51*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Tue, 11 Nov 2008 11:47:53 0800, Eric wrote:
I'm learning Python (while coming from MATLAB). One question I have is
that if I have a list with say 8 elements, and I want just a few of them
how do I select them out. In MATLAB, if I just want the first, fifth and
eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting
specific indices. Any suggestions?
b = [a[i] for i in [1, 5, 8]]
Ciao,
* * * * Marc 'BlackJack' Rintsch
Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.
Regards,
Eric  
P: n/a

Guilherme Polo wrote:
On Tue, Nov 11, 2008 at 5:47 PM, Eric <er********@gmail.comwrote:
>I'm learning Python (while coming from MATLAB). One question I have is that if I have a list with say 8 elements, and I want just a few of them how do I select them out. In MATLAB, if I just want the first, fifth and eighth element I might do something like this:
b = a([1 5 8]);
I can't seem to figure out a similar Python construct for selecting specific indices. Any suggestions?
MATLAB works with 1based index, while Python is 0based, so accessing
the index number 8 wouldn't be valid with 8 elements here in Python.
Now, to solve your problem you could either use the already suggested
answer above mine or depending on what you what else you wanna do, you
will feel more comfortable using numpy.
Supposing you have a array with 8 elements: x = numpy.arange(8)
To pull out the first, fifth and eighth elements you would do:
x[numpy.array([0, 4, 7])]
Actually, x[[0,4,7]] will work just as well.

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  
P: n/a

Eric wrote:
On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
>On Tue, 11 Nov 2008 11:47:53 0800, Eric wrote:
>>I'm learning Python (while coming from MATLAB). One question I have is that if I have a list with say 8 elements, and I want just a few of them how do I select them out. In MATLAB, if I just want the first, fifth and eighth element I might do something like this: b = a([1 5 8]); I can't seem to figure out a similar Python construct for selecting specific indices. Any suggestions?
b = [a[i] for i in [1, 5, 8]]
Ciao, Marc 'BlackJack' Rintsch
Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.
In my experience, I never do this with lists so there's no optimized syntax for
it. I do use it very often with numpy arrays, and that does have optimized syntax.

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  
P: n/a

Eric wrote:
... In MATLAB, if I just want the first, fifth and eighth element I
might do something like this: b = a([1 5 8]);
On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netreplied:
>b = [a[i] for i in [1, 5, 8]]
To which Eric said:
Thanks! It makes sense, but in this case MATLAB seems easier and no
less readable. That said, I know better than for a newbie like me to
question syntax issues.
If you are using numpy, you may find found the following link useful: http://www.scipy.org/NumPy_for_Matlab_Users
Scott David Daniels Sc***********@Acm.Org   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1445
 replies: 6
 date asked: Nov 11 '08
