Created
October 19, 2012 18:43
-
-
Save aaronsaunders/3919920 to your computer and use it in GitHub Desktop.
recipes for groupers from http://docs.python.org/library/itertools.html#recipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def grouperchunker(seq, size): | |
return (seq[pos:pos + size] for pos in xrange(0, len(seq), size)) | |
for group in grouper(animals, 3): | |
print group | |
from itertools import | |
def grouper(n, iterable, fillvalue=None): | |
"Collect data into fixed-length chunks or blocks" | |
# grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx | |
args = [iter(iterable)] * n | |
return izip_longest(fillvalue=fillvalue, *args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment