Skip to content

Instantly share code, notes, and snippets.

@aodag
Created January 14, 2015 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aodag/9bd3a807f603b17b413d to your computer and use it in GitHub Desktop.
Save aodag/9bd3a807f603b17b413d to your computer and use it in GitHub Desktop.
先頭からN個ずつの要素でスライスしていくジェネレータ: itertoolsにありそうなのにないっぽい
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