I know this question has been covered many times but my requirement is different.
I have a list like: range(1, 26)
. I want to divide this list into a fi
If you want to have the chunks as evenly sized as possible:
def chunk_ranges(items: int, chunks: int) -> List[Tuple[int, int]]:
"""
Split the items by best effort into equally-sized chunks.
If there are fewer items than chunks, each chunk contains an item and
there are fewer returned chunk indices than the argument `chunks`.
:param items: number of items in the batch.
:param chunks: number of chunks
:return: list of (chunk begin inclusive, chunk end exclusive)
"""
assert chunks > 0, \
"Unexpected non-positive chunk count: {}".format(chunks)
result = [] # type: List[Tuple[int, int]]
if items <= chunks:
for i in range(0, items):
result.append((i, i + 1))
return result
chunk_size, extras = divmod(items, chunks)
start = 0
for i in range(0, chunks):
if i < extras:
end = start + chunk_size + 1
else:
end = start + chunk_size
result.append((start, end))
start = end
return result
Test case:
def test_chunk_ranges(self):
self.assertListEqual(chunk_ranges(items=8, chunks=1),
[(0, 8)])
self.assertListEqual(chunk_ranges(items=8, chunks=2),
[(0, 4), (4, 8)])
self.assertListEqual(chunk_ranges(items=8, chunks=3),
[(0, 3), (3, 6), (6, 8)])
self.assertListEqual(chunk_ranges(items=8, chunks=5),
[(0, 2), (2, 4), (4, 6), (6, 7), (7, 8)])
self.assertListEqual(chunk_ranges(items=8, chunks=6),
[(0, 2), (2, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
self.assertListEqual(
chunk_ranges(items=8, chunks=7),
[(0, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
self.assertListEqual(
chunk_ranges(items=8, chunks=9),
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8)])
x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
chunk = len(x)/6
l=[]
i=0
while i<len(x):
if len(l)<=4:
l.append(x [i:i + chunk])
else:
l.append(x [i:])
break
i+=chunk
print l
#output=[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24, 25]]
Assuming you want to divide into n chunks:
n = 6
num = float(len(x))/n
l = [ x [i:i + int(num)] for i in range(0, (n-1)*int(num), int(num))]
l.append(x[(n-1)*int(num):])
This method simply divides the length of the list by the number of chunks and, in case the length is not a multiple of the number, adds the extra elements in the last list.
One way would be to make the last list uneven and the rest even. This can be done as follows:
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
>>> m = len(x) // 6
>>> test = [x[i:i+m] for i in range(0, len(x), m)]
>>> test[-2:] = [test[-2] + test[-1]]
>>> test
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24, 25]]
Use numpy
>>> import numpy
>>> x = range(25)
>>> l = numpy.array_split(numpy.array(x),6)
or
>>> import numpy
>>> x = numpy.arange(25)
>>> l = numpy.array_split(x,6);
You can also use numpy.split but that one throws in error if the length is not exactly divisible.
arr1=[-20, 20, -10, 0, 4, 8, 10, 6, 15, 9, 18, 35, 40, -30, -90, 99]
n=4
final = [arr1[i * n:(i + 1) * n] for i in range((len(arr1) + n - 1) // n )]
print(final)
Output:
[[-20, 20, -10, 0], [4, 8, 10, 6], [15, 9, 18, 35], [40, -30, -90, 99]]