How is this list comprehension evaluated?

A

Arturo B

Hello, I'm making Python mini-projects and now I'm making a Latin Square

(Latin Square: http://en.wikipedia.org/wiki/Latin_square)

So, I started watching example code and I found this question on Stackoverflow:

http://stackoverflow.com/questions/...-permutations-reduced-latin-squares-in-python

It uses a list comprenhension to generate the Latin Square, I'm am a newbie to Python, and I've tried to figure out how this is evaluated:

a = [1, 2, 3, 4]
n = len(a)
[[a[i - j] for i in range(n)] for j in range(n)]

I don't understand how the "i" and the "j" changes.
On my way of thought it is evaluated like this:

[[a[0 - 0] for 0 in range(4)] for 0 in range(4)]
[[a[1 - 1] for 1 in range(4)] for 1 in range(4)]
[[a[2 - 2] for 2 in range(4)] for 2 in range(4)]
[[a[3 - 3] for 3 in range(4)] for 3 in range(4)]

But I think I'm wrong... So, could you explain me as above? It would help me a lot.

Thanks for reading!
 
A

Antoon Pardon

Op 16-09-13 15:43, Arturo B schreef:
Hello, I'm making Python mini-projects and now I'm making a Latin Square

(Latin Square: http://en.wikipedia.org/wiki/Latin_square)

So, I started watching example code and I found this question on Stackoverflow:

http://stackoverflow.com/questions/...-permutations-reduced-latin-squares-in-python

It uses a list comprenhension to generate the Latin Square, I'm am a newbie to Python, and I've tried to figure out how this is evaluated:

a = [1, 2, 3, 4]
n = len(a)
[[a[i - j] for i in range(n)] for j in range(n)]

I don't understand how the "i" and the "j" changes.
On my way of thought it is evaluated like this:

[[a[0 - 0] for 0 in range(4)] for 0 in range(4)]
[[a[1 - 1] for 1 in range(4)] for 1 in range(4)]
[[a[2 - 2] for 2 in range(4)] for 2 in range(4)]
[[a[3 - 3] for 3 in range(4)] for 3 in range(4)]

But I think I'm wrong... So, could you explain me as above? It would help me a lot.

Thanks for reading!

Just start your python interpreter and type the following
[[(i,j) for i in range(3)] for j in range(3)]

That should give you a clue.
 
M

Michael Torrie

It uses a list comprenhension to generate the Latin Square, I'm am a newbie to Python, and I've tried to figure out how this is evaluated:

a = [1, 2, 3, 4]
n = len(a)
[[a[i - j] for i in range(n)] for j in range(n)]

I don't understand how the "i" and the "j" changes.
On my way of thought it is evaluated like this:

It helps to convert it to a conventional for loop to see how it works:

a = [1, 2, 3, 4]
n = len(a)

resultj = []

for j in range(n):
resulti = []

for i in range(n):
resulti.append(a[i-j])

resultj.append(resulti)
 
R

Roy Smith

Arturo B said:
Hello, I'm making Python mini-projects and now I'm making a Latin Square

(Latin Square: http://en.wikipedia.org/wiki/Latin_square)

So, I started watching example code and I found this question on
Stackoverflow:

http://stackoverflow.com/questions/5313900/generating-cyclic-permutations-redu
ced-latin-squares-in-python

It uses a list comprenhension to generate the Latin Square, I'm am a newbie
to Python, and I've tried to figure out how this is evaluated:

a = [1, 2, 3, 4]
n = len(a)
[[a[i - j] for i in range(n)] for j in range(n)]

You can re-write any list comprehension as a for loop. In this case you
have to un-wrap this one layer at a time. First step:

a = [1, 2, 3, 4]
n = len(a)
temp1 = []
for j in range(n):
temp2 = [a[i - j] for i in range(n)]
temp1.append(item)

then, unwrap the next layer:

a = [1, 2, 3, 4]
n = len(a)
temp1 = []
for j in range(n):
temp2 = []
for i in range(n):
temp3 = a[i - j]
temp2.append(temp3)
temp1.append(item)

Does that make it any easier to understand?
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top