473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple question about Python lists

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 1688
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
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 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])]

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/python-list


--
-- Guilherme H. Polo Goncalves
Nov 11 '08 #3
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
Nov 11 '08 #4
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 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
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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
13
by: Paulo Pinto | last post by:
Hi, does anyone know of a Python package that is able to load XML like the XML::Simple Perl package does? For those that don't know it, this package maps the XML file to a dictionary.
4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
19
by: gaudetteje | last post by:
I've been searching high and low for a way to simply convert a small XML configuration file to Python data structures. I came across gnosis XML tools, but need a built-in option for doing...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
30
by: Brian Elmegaard | last post by:
Hi, I am struggling to understand how to really appreciate object orientation. I guess these are FAQ's but I have not been able to find the answers. Maybe my problem is that my style and...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
7
by: Zethex | last post by:
Im a bit new to python. Anyway working on a little project of mine and i have nested lists ie Answer = , ] and so forth.., Anyway the amount of ] do increase over time. Im just wondering...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.