"The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip([iter(s)]n)"
Use itertools.izip (or Python 3.0!) to generate N-tuples.
Please kill this before it gets cut-n-pasted around. What you want is on the itertools docs page.
http://docs.python.org/library/itertools.html#module-itertools
from itertools import groupby
N = range(10)
groupevery = 4
[list(x[1]) for x in groupby(N, lambda x: x // groupevery)]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
Comments
Subscribe on this post's comments
permalink
http://docs.python.org/library/functions.html#zip
"The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip([iter(s)]n)"
Use itertools.izip (or Python 3.0!) to generate N-tuples.
permalink
Please kill this before it gets cut-n-pasted around. What you want is on the itertools docs page. http://docs.python.org/library/itertools.html#module-itertools
This does less work in general and also works with infinite iterators. And thank you for making me learn a markup language just to post this comment.
permalink
permalink
Thanks for your comments guys, you've opened my eyes :)
If you wish to leave comment, please, identify yourself and then come back to this page.