list comprehensions

Elaine Jackson elainejackson7355 at home.com
Thu Apr 8 01:05:43 EDT 2004


I still need to digest it, but yes, I think it will help. Thanks.

"Paul McGuire" <ptmcg at austin.rr._bogus_.com> wrote in message
news:ku0dc.74742$o_.33729 at fe1.texas.rr.com...
| "Elaine Jackson" <elainejackson7355 at home.com> wrote in message
| news:yjZcc.42823$Pk3.9547 at pd7tw1no...
| >
| > List comprehensions don't work the way you intuitively expect them to
| work. I
| > realize many people have no intuitions about how list comprehensions
| 'should'
| > work, so if you recognize yourself in this description, feel free to go
| back to
| > whatever you were doing before. If you're still here, though, I invite you
| to
| > consider the following definition:
| >
| > partitions = lambda n: [[n]]+[[k]+x for x in partitions(n-k) for k in
| > range(1,n)]
| >
| > As defined above, the function raises an exception when you call it ('k'
| > referenced before assignment). For the sake of clarity, here is workable
| code
| > expressing the same intention:
| >
| > def partitions(n):
| >     reVal=[[n]]
| >     for k in range(1,n):
| >         for x in partitions(n-k):
| >             reVal.append([k]+x)
| >     return reVal
| >
| > So I guess what I want to ask is: Can somebody explain the semantics of
| list
| > comprehensions to me? Or even better: Can somebody tell me where to look
| in the
| > documentation to find out about list comprehensions? All donations
| gratefully
| > received.
| >
| > Peace
| >
| >
| Elaine -
|
| I suspect you wrote your listcomp by thinking this is an inversion of the
| nested for loops.  That is, if you think of some arbitrary nesting of loops
| as:
|
|     for i in firstRange:
|         for j in secondRange:
|             for k in thirdRange:
|                 ...<as many more fors as you like>...
|                     <build up list using i,j,k,...>
|
| then you looked at the list comp as working its way inside out, since it
| starts with the <build up list> part.
|
| But as the other posters have already stated, even though the listcomp
| starts with the most deeply nested body, the remaining 'for... for ...
| for... ' clauses match the nested loops, as if you had just removed the
| colons and copied them all onto one line.  So the listcomp ends up looking
| like:
|
|      [<build up list using i,j,k,...> for i in firstRange for j in
| secondRange for k in thirdRange...]
|
| If you want some kind of model for how to conceptualize this, then  I'd say
| that a listcomp starts with its most important part being the actual list
| element assembly/definition, followed by the iteration specifiers in
| successive outer-to-inner order, that will get the loop variables to the
| necessary values to satisfy the list assembly code.
|
| You also asked for doc references.  The Python Tutorial includes an obscure
| example (you have do a bit of in-your-head multiplication to get it), but
| try these other articles describing listcomps when they were a new feature:
| http://www-106.ibm.com/developerworks/linux/library/l-py20.html
| http://python.org/2.0/new-python.html#SECTION000600000000000000000
| Perhaps the tutorial could lift some of the examples from these other pages,
| as I'm sure people don't necessarily go back to the "What's New" pages for
| version x-2, x-3, etc.
|
| HTH,
| -- Paul
|
|
|
|





More information about the Python-list mailing list