homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mattlong
Recipients docs@python, mattlong
Date 2011年12月07日.21:49:53
SpamBayes Score 1.1521819e-06
Marked as misclassified No
Message-id <1323294594.34.0.721210305301.issue13549@psf.upfronthosting.co.za>
In-reply-to
Content
The description of nesting list comprehensions in section 5.1.5 of the main Python tutorial (http://docs.python.org/tutorial/datastructures.html#nested-list-comprehensions) is misleading at best and arguably incorrect. Where it says "To avoid apprehension when nesting list comprehensions, read from right to left." This is incorrect and conflicts directly with the comment at the bottom of PEP 202 (http://www.python.org/dev/peps/pep-0202/), which says the last index varies fastest.
Take the following example:
matrix = [[1,2],[3,4],[5,6]]
my_list = []
for row in matrix:
 for number in row
 my_list.append(number)
The current documentation would suggest I do the following to achieve the same result with list comprehensions since the for statements should be read from right to left:
matrix = [[1,2],[3,4],[5,6]]
my_list = [number for number in row for row in matrix]
Running this code will result in an error. The correct form is:
matrix = [[1,2],[3,4],[5,6]]
[number for row in matrix for number in row]
Clearly the nesting order only matters when the inner loop depends on the outer loop as in my example above. There is no such dependency in the documentation's example, which is why it is does not result in an Error.
History
Date User Action Args
2011年12月07日 21:49:54mattlongsetrecipients: + mattlong, docs@python
2011年12月07日 21:49:54mattlongsetmessageid: <1323294594.34.0.721210305301.issue13549@psf.upfronthosting.co.za>
2011年12月07日 21:49:53mattlonglinkissue13549 messages
2011年12月07日 21:49:53mattlongcreate

AltStyle によって変換されたページ (->オリジナル) /