Skip to content

Instantly share code, notes, and snippets.

@codeb2cc
Last active December 19, 2015 04:48
Show Gist options
  • Save codeb2cc/5899399 to your computer and use it in GitHub Desktop.
Save codeb2cc/5899399 to your computer and use it in GitHub Desktop.
Iterable segregator
from itertools import chain, tee, izip
# A
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
arr = range(100)
step = 7
# [0, 1, 2, 3, ... 99] -> [(0, 7), (7, 14), ..., (98, 100)]
pairwise(chain(xrange(0, len(arr), step), (len(arr), )))
# B
izip(xrange(0, 90, 10), xrange(10, 100, 10))
# C
(arr[i:i + 10] for i in xrange(0, 100, 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment