Last active
August 29, 2015 13:58
-
-
Save mittenchops/9962536 to your computer and use it in GitHub Desktop.
There are other ways of doing this, this is the one I used for where I was working with this for something that parallelized easily.
This file contains 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 __future__ import print_function | |
import numpy | |
import random | |
mylist = range(0,100) | |
random.shuffle(mylist) | |
myarray = numpy.array(mylist) | |
# via http://stackoverflow.com/questions/2130016/splitting-a-list-of-arbitrary-size-into-only-roughly-n-equal-parts | |
def chunkit(seq, N): | |
""" | |
Prints the bounds of a list divided into at most N chunks. | |
This is simple but useful in the case of spawning lots of workers from the | |
command line, where you need to process (0, x), (x+1, 2x+1), etc. records at a time. | |
and where you're going to have some irregular divisions. | |
""" | |
avg = len(seq) / float(N) | |
out = [] | |
last = 0.0 | |
while last < len(seq): | |
out.append((int(last), min(int(last + avg), len(seq)))) | |
last += (avg + 1) | |
return(out) | |
f = lambda x: print("f --lower {} --higher {}".format(x[0],x[1])) | |
bounds = chunkit(mylist, 6) | |
print(bounds) | |
for b in bounds: | |
f(b) | |
print("and with map") | |
map(f,bounds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment