Created
January 14, 2015 01:01
-
-
Save aodag/9bd3a807f603b17b413d to your computer and use it in GitHub Desktop.
先頭からN個ずつの要素でスライスしていくジェネレータ: itertoolsにありそうなのにないっぽい
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 part_seq(seq, num): | |
""" | |
>>> list(part_seq(range(10), 5)) | |
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]] | |
>>> list(part_seq(range(10), 3)) | |
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] | |
""" | |
s = seq | |
while s: | |
f, s = s[:num], s[num:] | |
yield f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment