Skip to content

Instantly share code, notes, and snippets.

@stober
Created January 25, 2013 04:40
Show Gist options
  • Save stober/4631823 to your computer and use it in GitHub Desktop.
Save stober/4631823 to your computer and use it in GitHub Desktop.
Slice indices for generating array chunks.
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