Ezio Melotti <[email protected]> added the comment:
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?
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue13549>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com