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


Groups > comp.lang.python > #54227

Re: How is this list comprehension evaluated?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <antoon.pardon@rece.vub.ac.be>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.019
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'python,': 0.02; 'interpreter': 0.05; 'newbie': 0.05; 'received:134': 0.05; '[1,': 0.09; 'subject:skip:c 10': 0.09; 'subject:How': 0.10; 'python': 0.11; 'above?': 0.16; '>>>': 0.22; 'example': 0.22; 'header:User- Agent:1': 0.23; 'question': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'subject:list': 0.30; "i'm": 0.30; 'code': 0.31; 'url:wiki': 0.31; 'lot.': 0.31; 'url:wikipedia': 0.31; 'figure': 0.32; 'could': 0.34; 'but': 0.35; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'making': 0.63; 'watching': 0.68; 'square': 0.74; 'subject:this': 0.83; '"j"': 0.84; 'pardon': 0.84; 'reading!': 0.84; 'square,': 0.84; 'wrong...': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result Ap4EANMMN1KGuA9G/2dsb2JhbABagz/BSYEygxoBBYEJCyElDwJGEwgCh38MsSGIHI96FoQIA5d7gS+EbYtYgyY
Date Mon, 16 Sep 2013 15:53:33 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130116 Icedove/10.0.12
MIME-Version 1.0
To python-list@python.org
Subject Re: How is this list comprehension evaluated?
References <eae87c72-f62d-4815-bb69-ca862ff78f1e@googlegroups.com>
In-Reply-To <eae87c72-f62d-4815-bb69-ca862ff78f1e@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.29.1379339615.18130.python-list@python.org> (permalink)
Lines 36
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379339615 news.xs4all.nl 16004 [2001:888:2000:d::a6]:37369
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54227

Show key headers only | View raw


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

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