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 ezio.melotti
Recipients docs@python, eric.araujo, ezio.melotti, mark.dickinson, mattlong, terry.reedy
Date 2011年12月09日.13:44:39
SpamBayes Score 6.845855e-08
Marked as misclassified No
Message-id <1323438280.79.0.520887157014.issue13549@psf.upfronthosting.co.za>
In-reply-to
Content
In the first patch I included this example:
+ >>> swapped = []
+ >>> for i in [0, 1, 2]:
+ ... swapped.append([row[i] for row in mat])
+ ...
+ >>> print swapped
+ [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Because I applied the following transformation without looking at what expr was (another listcomp in this case):
 res = [expr for elem in seq]
==>
 res = []
 for elem in seq:
 res.append(expr)
If the reader does the same he/she wouldn't have any problem figuring out what is the order of the `for`s, but the second version of the patch only includes a "fully expanded" example:
+ >>> transposed = []
+ >>> for i in range(4):
+ ... # the following 3 lines implement the nested listcomp
+ ... transposed_row = []
+ ... for row in matrix:
+ ... transposed_row.append(row[i])
+ ... transposed.append(transposed_row)
+ ...
+ >>> transposed
Here it's easier to confuse the two `for` (not because they are similar, but because there are two of them, whereas in the previous example there's only one).
I added a comment to clarify that the inner loop is the listcomp, but maybe it would be better to show the first example too, followed by the "fully expanded" one, i.e.:
+ [[row[i] for row in mat] for i in range(4)]
+ >>> transposed = []
+ >>> for i in range(4):
+ ... transposed.append([row[i] for row in mat])
+ ...
+ >>> transposed
+ >>> transposed = []
+ >>> for i in range(4):
+ ... # the following 3 lines implement the nested listcomp
+ ... transposed_row = []
+ ... for row in matrix:
+ ... transposed_row.append(row[i])
+ ... transposed.append(transposed_row)
+ ...
+ >>> transposed
The step in the middle shows that in order to get to the "fully expanded" form, it's enough to apply the "usual" listcomp->for+append transformation twice, without worrying about left-to-right vs right-to-left. Do you think it's worth adding this?
History
Date User Action Args
2011年12月09日 13:44:40ezio.melottisetrecipients: + ezio.melotti, terry.reedy, mark.dickinson, eric.araujo, docs@python, mattlong
2011年12月09日 13:44:40ezio.melottisetmessageid: <1323438280.79.0.520887157014.issue13549@psf.upfronthosting.co.za>
2011年12月09日 13:44:40ezio.melottilinkissue13549 messages
2011年12月09日 13:44:39ezio.melotticreate

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