hi all,
what is the best way to divide a list into roughly equal parts? for example, if I have a list with 54 elements and i want to split it into 3 roughly equal parts? I'd like the parts to be as even as possible, hopefully assigning the elements that do not fit in a way that gives the most equal parts. to give a concrete case, if the list has 7 elements and I want to split it into 2 parts, I'd ideally want to get the first 3 elements in one bin, the second should have 4 elements.
so to summarize I'm looking for something like even_split(l, n) that breaks l into roughly n-different parts.
the best I can think of is something that was posted here:
def chunks(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
example:
l = range(54)
chunks_of_three = chunks(l, 3)
but this gives chunks of 3, rather than 3 equal parts. I could simply iterate over this and take the first element of each column, call that part one, then take the second and put it in part two, etc. but that seems inefficient and inelegant. it also breaks the order of the list.
any ideas on this?
thanks.