455,587 Members | 1,580 Online
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

 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 Nov 11 '08 #1
6 Replies

 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 Nov 11 '08 #2

 P: n/a On Tue, Nov 11, 2008 at 5:47 PM, Eric

 P: n/a On Nov 11, 1:51*pm, Marc 'BlackJack' Rintsch

 P: n/a Guilherme Polo wrote: On Tue, Nov 11, 2008 at 5:47 PM, Eric I'm learning Python (while coming from MATLAB). One question I have isthat if I have a list with say 8 elements, and I want just a few ofthem 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 selectingspecific indices. Any suggestions? MATLAB works with 1-based index, while Python is 0-based, 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 Nov 11 '08 #5

 P: n/a Eric wrote: On Nov 11, 1:51 pm, Marc 'BlackJack' Rintsch On Tue, 11 Nov 2008 11:47:53 -0800, Eric wrote: >>I'm learning Python (while coming from MATLAB). One question I have isthat if I have a list with say 8 elements, and I want just a few of themhow do I select them out. In MATLAB, if I just want the first, fifth andeighth element I might do something like this:b = a([1 5 8]);I can't seem to figure out a similar Python construct for selectingspecific 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 Nov 11 '08 #6

 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 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 Nov 12 '08 #7