By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,537 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,537 IT Pros & Developers. It's quick & easy.

list comprehensions

P: n/a

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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Elaine Jackson wrote:
List comprehensions don't work the way you intuitively expect them to work. I
How can you say such a thing. 100 Haskell programmers have been asked
about Python list comprehension and all found it intuitive.
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).
Try a simpler example and you'll see the order of execution with nested
loops:
[(a, x) for a in "ab" for x in "xy"] [('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y')]
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.


http://www.python.org/doc/2.3.3/ref/lists.html, especially "each of the
for or if clauses a block, nesting from left to right".
partitions = lambda n: [[n]]+[[k]+x for k in range(1,n) for x in partitions(n-k)] partitions (3)

[[3], [1, 2], [1, 1, 1], [2, 1]]

Daniel
Jul 18 '05 #2

P: n/a

"Elaine Jackson" <el***************@home.com> wrote in message
news:yjZcc.42823$Pk3.9547@pd7tw1no...

List comprehensions don't work the way you intuitively expect them to work.

One of my pet posting peeves is when people ascribe their own
characteristics to others, as when they write 'you' when they really mean,
or should have meant, 'I'. But nevermind.
I realize many people have no intuitions about how list comprehensions 'should' work,
List comprehensions are sufficiently 'artificial' that I would advise
anyone to avoid the temptation to intuit how they work without consulting
the manual, tutorials, or other written explanations.

The Python Reference Manual Index, section L, entry List..comprehensions
takes one to subsection 5.2.4 List displays. The last two sentences
therein read

"When a list comprehension is supplied, it consists of a single expression
followed by at least one for clause and zero or more for or if clauses. In
this case, the elements of the new list are those that would be produced by
considering each of the for or if clauses a block, nesting from left to
right, and evaluating the expression to produce a list element each time
the innermost block is reached."

OK, takes a couple of readings and maybe some working examples.
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).
Of course, you have the two for clauses reversed, as the error message
might suggest to an experienced Pythoneer. As the manual specifies, for/if
clauses nest *left to right*. To match the double loop below, you want
instead

[[k]+x for k in range(1,n) for x in partitions(n-k)]
def partitions(n):
reVal=[[n]]
for k in range(1,n):
for x in partitions(n-k):
reVal.append([k]+x)
return reVal


Terry J. Reedy


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.