Skip to content

Instantly share code, notes, and snippets.

@hirokiky
Last active December 20, 2015 12:39
Show Gist options
  • Save hirokiky/6132839 to your computer and use it in GitHub Desktop.
Save hirokiky/6132839 to your computer and use it in GitHub Desktop.
[0, 1, 2, 3, 4, 5] => [[0, 1], [2, 3], [4, 5]]
chunker1 = lambda xs: zip(xs[:-1:2], xs[1::2])
chunker2 = lambda xs: [xs[i:i+2] for i in range(0,len(xs),2)]
def chunker3(xs):
i = 0
while xs[i:i+2]:
yield xs[i:i+2]
i += 2
def chunker4(xs):
t = xs[::-1]
while t:
a = t.pop()
b = t.pop()
yield a, b
chunker5 = lambda xs: (lambda xs, size: zip(*tuple(xs[i::size] for i in range(size))))(xs, 2)
@hirokiky
Copy link
Author

hirokiky commented Aug 1, 2013

要素数が奇数の場合の挙動が、それぞれ若干違う

@hirokiky
Copy link
Author

hirokiky commented Aug 1, 2013

これ素晴らしい https://gist.github.com/knzm/6133057

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment