0

Given an array and the number of almost equally sized parts that you want it divided into, is there a preferred way of doing so or a built-in that will handle the task? Two example implementations are given below, but someone may know a better method of getting the job done. The code should primarily be designed to array-like objects but could optionally be applicable if given an iterable instead.

Example 1

def partition(array, parts):
    """Takes an array and splits it into roughly equally sized parts."""
    array_size = len(array)
    part_size = math.ceil(array_size / parts)
    for offset in range(0, array_size, part_size):
        yield array[offset:offset + part_size]

Example 2

def partition(array, parts):
    """Takes an array and splits it into roughly equally sized parts."""
    size = math.ceil(len(array) / parts)
    for stop, start in enumerate(range(parts), 1):
        yield array[start * size:stop * size]
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • This is not an exact duplicate of [Python: Slicing a list into n nearly-equal-length partitions](http://stackoverflow.com/questions/2659900/python-slicing-a-list-into-n-nearly-equal-length-partitions) since one question covers functions and the other covers generators. This question also asks for the merits of different implementations and (hopefully) will receive an answer regarding generators that come in the standard library. – Noctis Skytower Nov 30 '16 at 20:03
  • 1
    Every answer there can be trivially converted to generator functions (or more concisely, to just return generator expressions). Converting to returning generator expressions is just changing list comp brackets to genexpr parens. If you don't want the generator expression, you just unroll the list comp into a for loop with `yield`; not sufficiently distinct. The answers & comments there cover the merits of different implementations, and given that the Pythonic solution requested by the OP would be to use a standard library function if it existed, it would be one of the answers, and it isn't. – ShadowRanger Nov 30 '16 at 20:08
  • @ShadowRanger What about the two example implementations provided in this question? They are completely unlike any of the answers to the question you marked as a duplicate. Should I ask two new questions for each of them to get feedback? – Noctis Skytower Nov 30 '16 at 20:13
  • 2
    The reason your examples aren't found there is because your examples produce bad results. The accepted answer there used to behave like yours, where partitioning 105 elements into 10 partitions would produce nine partitions of size 11 and one of size 6, but that's not "nearly equal". "Nearly equal" would be five partitions of size 11 and five more of size 10, which is what the answers there correctly do. That problem is mentioned in the comments, and visible in the answer edit history. – ShadowRanger Nov 30 '16 at 20:18
  • @ShadowRanger Thanks for the explanation! That explains why the `round` function is used in several answers. They make better sense now. – Noctis Skytower Nov 30 '16 at 20:21
  • 1
    You're welcome. If you think your solutions are better, go ahead and post them as an answer over there; they're perfectly fine alternate answers, just not the basis for a new question. – ShadowRanger Nov 30 '16 at 20:21

0 Answers0