Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54229

Re: How is this list comprehension evaluated?

Date 2013-09-16 08:20 -0600
From Michael Torrie <torriem@gmail.com>
Subject Re: How is this list comprehension evaluated?
References <eae87c72-f62d-4815-bb69-ca862ff78f1e@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.31.1379341258.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web