Loop in list.

J

Jim

Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.
 
F

Fredrik Lundh

Jim said:
Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.

it's called "list comprehension", and was added in Python 2.0.

http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000
http://docs.python.org/tut/node7.html#SECTION007140000000000000000
http://docs.python.org/ref/lists.html

Python 2.4 introduces a variation called "generator expressions":

http://www.python.org/doc/2.4/whatsnew/node4.html
http://www.python.org/peps/pep-0289.html
http://docs.python.org/ref/genexpr.html

</F>
 
S

Simon Brunning

Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.

It's called a "List Comprehension". There's a good historical reason
for the name, but you can be excused in not having looked under that
name in the index. See
<http://www.amk.ca/python/2.0/index.html#SECTION000600000000000000000>
for details.

They *should* be mentioned in most books, provided that it's vaguely
recent and covers Python 2.0 or later.

List comps have a cool new little sister, generator expressions - see
<http://www.brunningonline.net/simon/blog/archives/001025.html>.
 
D

Diez B. Roggisch

Jim said:
Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.

Its called a list-comprehension. And as I don't know what books you mean, I
can't say if its covered there or not.
 
R

Roy Smith

Jim said:
Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.

It's a list comprehension. Unfortunately, this is a bad example of
one, since a much simplier way of writing the same thing would be:

mat = ['a'] * 3

After several years of them being in the language, I'm still somewhat
up in the air about the wisdom of list comprehensions. There is no
doubt that they're convenient and compact, but I think they make code
more difficult to read. The functional programming folks will
certainly disagree, and I realize I'm probably in the minority on
this, but that's my opinion.
 
J

Jim

Thanks for the help. Python is somewhat mysterious to an old fortan
programer.

Jim
 
S

Stephen Thorne

'>>> a = [i*2*b for i in range(3) for b in range(4)]
'>>> a
[0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]

Might take you a while to correlate the answer with the loop, but you
should be able to see after a while that this nesting is the same as

'>>> a = []
'>>> for b in range(4):
'>>> for i in range(3):
'>>> a.append(i*2*b)

There is a subtle error in this explanation. The equivilence actually
looks like:

'> a = []
'> l1 = range(4)
'> l2 = range(3)
'> for b in l1:
'> for i in l2:
'> a.append(i*2*b)

Stephen
 
B

Bruno Desthuilliers

Jim a écrit :
Where did this type of structure come from:

mat = ['a' for i in range(3)] ?

This will produce a list of three elements but
I don't see reference for it in any of the books.
Now everyone told you *what* is it, I'll (very very dumbly) answer the
question : this syntax comes from Haskell.

HTH !-)
Bruno
 
F

Fredrik Lundh

Stephen said:
'>>> a = [i*2*b for i in range(3) for b in range(4)]
'>>> a
[0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]

Might take you a while to correlate the answer with the loop, but you
should be able to see after a while that this nesting is the same as

'>>> a = []
'>>> for b in range(4):
'>>> for i in range(3):
'>>> a.append(i*2*b)

There is a subtle error in this explanation.

if you run the example, you'll notice that it's not so subtle. read on.
The equivilence actually looks like:

'> a = []
'> l1 = range(4)
'> l2 = range(3)
'> for b in l1:
'> for i in l2:
'> a.append(i*2*b)

really?

def myrange(x):
print "RANGE", x
return range(x)

print [i*2*b for i in myrange(3) for b in myrange(4)]

a = []
for b in myrange(4):
for i in myrange(3):
a.append(i*2*b)
print a

a = []
l1 = myrange(4)
l2 = myrange(3)
for b in l1:
for i in l2:
a.append(i*2*b)
print a

prints

RANGE 3
RANGE 4
RANGE 4
RANGE 4
[0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]
RANGE 4
RANGE 3
RANGE 3
RANGE 3
RANGE 3
[0, 0, 0, 0, 2, 4, 0, 4, 8, 0, 6, 12]
RANGE 4
RANGE 3
[0, 0, 0, 0, 2, 4, 0, 4, 8, 0, 6, 12]

(to translate a list comprehension to nested statements, remove
the result expression, insert colons and newlines between the for/if
statement parts, and put the append(result expression) part inside
the innermost statement)

</F>
 
C

Caleb Hattingh

Jim

Someone on this list (SteveB) helped me quite a bit with a list
comprehension on a recent thread. Roy said it can be hard to read, and I
agree in part because I always thought they were hard to read, when in
actual fact I had just never bothered to learn properly. Here is a
mini-tutorial:

e.g. 1: The theory

'>>> a = [ <item1> for i in <item2> ]

item2 is iterable (like "range()" in your example)
item1 is the thing that is going to fill up the resulting list, and item1
is evaluated at each step of the "for" loop.

This is the same as
'>>> a = []
'>>> for i in <item2>:
'>>> a.append(<item1>)

e.g. 2: A real example

'>>> a = [i*2 for i in range(3)]
'>>> a
[0, 2, 4]

so "i*2" gets evaluated for each step in the "for" loop. The values of
"i" at each step are [0,1,2], according to how "range" works, so "i*2" is
what you end up with in the resulting list.

e.g. 3: They can be nested

'>>> a = [i*2*b for i in range(3) for b in range(4)]
'>>> a
[0, 0, 0, 0, 0, 2, 4, 6, 0, 4, 8, 12]

Might take you a while to correlate the answer with the loop, but you
should be able to see after a while that this nesting is the same as

'>>> a = []
'>>> for b in range(4):
'>>> for i in range(3):
'>>> a.append(i*2*b)

keep well
Caleb
 
C

Caleb Hattingh

Stephen

You're gonna have to help me here.....what is the effective difference?

Thanks
Caleb
'>>> a = []
'>>> for b in range(4):
'>>> for i in range(3):
'>>> a.append(i*2*b)

There is a subtle error in this explanation. The equivilence actually
looks like:

'> a = []
'> l1 = range(4)
'> l2 = range(3)
'> for b in l1:
'> for i in l2:
'> a.append(i*2*b)

Stephen
 
C

Caleb Hattingh

Hi Fredrik

*sigh* I think I will stop writing mini-tutorials :)

You are, of course, correct. And I really like your method of
explaining how to mentally juggle the LC into explicit loops.

I shudder to think how mnay people I confused with my incorrect
examples - I really should have tested them first.

Thanks again
Caleb
 
J

Jim

Wow! All I wanted to do was write the equivalence
of the Fortran statement: Real*4 matrix(n,n).

I'm going to have to go to the intrepreter to see what
your saying.

Thanks for all the help.

Jim
 
B

beliavsky

Jim said:
Wow! All I wanted to do was write the equivalence
of the Fortran statement: Real*4 matrix(n,n).

If you are doing numerical linear algebra in Python, you should use the
Numeric or Numarray modules. With Numeric, the equivalent is just

from Numeric import zeros
matrix = zeros([n,n],Float)
 
J

Jim

I did appreciate the reference. I started with Fortran
on an IBM (7040 maybe, not sure) using keypunched cards. Some of the
concepts of the newer languages take some to seem useable.

Jim
 
J

Jim

I assume this is one of the addons for Python. I know that there
is a great deal of stuff out there available for Python that does
some of the stuff that I am looking at, but I am interested in
learning to use Python. When I want to get faster and more
general, I will get some of this stuff or use a different language.

Thanks for the help.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top