Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54226 > unrolled thread
| Started by | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| First post | 2013-09-16 06:43 -0700 |
| Last post | 2013-09-16 20:04 -0400 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
How is this list comprehension evaluated? Arturo B <a7xrturodev@gmail.com> - 2013-09-16 06:43 -0700
Re: How is this list comprehension evaluated? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-09-16 15:53 +0200
Re: How is this list comprehension evaluated? Michael Torrie <torriem@gmail.com> - 2013-09-16 08:20 -0600
Re: How is this list comprehension evaluated? Roy Smith <roy@panix.com> - 2013-09-16 20:04 -0400
| From | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| Date | 2013-09-16 06:43 -0700 |
| Subject | How is this list comprehension evaluated? |
| Message-ID | <eae87c72-f62d-4815-bb69-ca862ff78f1e@googlegroups.com> |
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-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!
[toc] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2013-09-16 15:53 +0200 |
| Message-ID | <mailman.29.1379339615.18130.python-list@python.org> |
| In reply to | #54226 |
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/5313900/generating-cyclic-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. -- Antoon Pardon
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-09-16 08:20 -0600 |
| Message-ID | <mailman.31.1379341258.18130.python-list@python.org> |
| In reply to | #54226 |
On 09/16/2013 07:43 AM, Arturo B wrote:
> 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)
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-09-16 20:04 -0400 |
| Message-ID | <roy-E9EDD2.20043216092013@news.panix.com> |
| In reply to | #54226 |
In article <eae87c72-f62d-4815-bb69-ca862ff78f1e@googlegroups.com>,
Arturo B <a7xrturodev@gmail.com> wrote:
> 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?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web