Re:Python iteration
by grifaton on Wednesday April 25, 2012 @12:34PM
(#3889801)
I sometimes use zip as a poor man's matrix transposition operator:
[ Parent
| on HN ]
If you want to transpose a matrix-like list of lists, but the sizes don't all match up, you can use izip_longest from itertools and give it a fillvalue to "fill in" for the missing elements:
>>> m = [(1,2,3), (4,5,6), (7, 8)]
>>> zip(*m)
[(1, 4, 7), (2, 5, 8)] # Wrong
>>> from itertools import izip_longest
>>> list(izip_longest(*m, fillvalue=None))
[(1, 4, 7), (2, 5, 8), (3, 6, None)]
Re:Python iteration
by christianmann on Wednesday April 25, 2012 @12:53PM
(#3889890)
That's pretty.