Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

split a num list into sublists with a specific order

I have a list of numbers from range 1 to n and I want to split those numbers into x sublists where each sublist contains less than 30 numbers. The amount of sublists is the least amount i need depending on the total of numbers in a list so for example if I had 68 I’d need 3 sublists at least where the numbers are split between them. How would I go about splitting them so each sublist has the next number in the original list to get an output like this

a = []
for i in range(68):
   a.append(i)

#code

output:
[[1,6,7,12,13,18,19,24....][2,5,8,11,14,17,20,23....][3,4,9,10,15,16,21,22....]]

or if I need 4 sub lists it’d go like this

output:
[[1,8,9,16,17....][2,7,10,15,18][3,6,11,14,19][4,5,12,13,20,21....]]

edit: this isn’t the exact same question as Splitting a python list into 4 sub-lists that are as even as possible since I don’t want the iteration to go back to the beginning once it finishes a loop. I want it to start iterating again from the bottom like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[0,13,14,28,29,42,43,56,57]
[1,12,15,27,30,41,44,55,58]
[2,11,16,26,31,40,45,54,59]
[3,10,17,25,32,39,46,53,60]
[4,9,19,24,33,38,47,52,61]
[5,8,20,23,34,37,48,51,62]
[6,7,21,22,35,36,49,50,63,64]

>Solution :

Let’s say you have

N = 68 # Number of elements
M = 30 # Max list size

First the number of sublists:

count = math.ceil(N / M)

Or, without ceil:

count = (N + M - 1) // M

The pattern of where you put each element of range(N) goes like this:

0, 1, 2, ..., count - 1, count - 1, count - 2, ..., 1, 0, 0, 1, 2, ...

Every count elements, you flip the order. So you can make a flip every count elements:

subs = [[] for _ in range(count)]
for i in range(N):
    index = i % count
    if (i // count) % 2:
        index = count - index - 1
    subs[index].append(i + 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading