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]