Last active
December 19, 2015 04:48
-
-
Save codeb2cc/5899399 to your computer and use it in GitHub Desktop.
Iterable segregator
This file contains hidden or 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
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