Created
January 25, 2013 04:40
-
-
Save stober/4631823 to your computer and use it in GitHub Desktop.
Slice indices for generating array chunks.
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
def chunk(n, nchunks): | |
"""Return a list of slices that divide an array into approximately nparts.""" | |
d = n / nchunks | |
r = n - nchunks * d | |
indices = range(0,(d+1)*r,d+1) + range((d+1)*r, n+d, d) | |
indices[-1] = None | |
a,b = tee(indices) | |
next(b,None) | |
return izip(a,b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment