0

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

[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]
  • Do you want a random number from 0 to 68 in any sublist? and how many numbers do you want in each sublist? – Tula Magar Sep 07 '22 at 18:39
  • No I want it to iterate through the numbers and put them into x sublists in ascending order and start from top to bottom and then bottom to top and repeat if that makes sense. like the example I put at the bottom of the question. – Thomas vincent Sep 07 '22 at 18:43

2 Answers2

3

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)

You can also look at it the other way: the pattern of elements in a sublist is (up to some offset):

1, 2 * count, 2 * count + 1, 4 * count, 4 * count + 1, 6 * count, ...

You can express this as a nested list comprehension:

subs = [[j + (count - i if (j // count) % 2 else i + 1) for j in range(0, N - i, count)] for i in range(count)]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Isn't the count simpler as `N // M + 1` - or am I missing something? – Mortz Sep 07 '22 at 19:06
  • 1
    @Mortz. If N % M == 0, that won't be right. – Mad Physicist Sep 07 '22 at 19:07
  • Ah! Good catch! – Mortz Sep 07 '22 at 19:08
  • @Mortz. Better yet, I think I figured out how to make this whole thing into a list comprehension – Mad Physicist Sep 07 '22 at 19:09
  • Yeah, I was trying that too, with `reversed` and `zip(*sublist)` - but I gave up :) – Mortz Sep 07 '22 at 19:11
  • Irrelevant to the initial question but is there an easy way I can name each list like the 1st one being sublist 1 the 2nd being sublist 2 etc so I can put them in variables and use each one separately if I need to? – Thomas vincent Sep 07 '22 at 19:11
  • @Mortz. There's no need. If you look at the pattern, it's 2*count, 2*count + 1, 4*count, 4*count + 1, 6*count, 6*count +1, ... – Mad Physicist Sep 07 '22 at 19:12
  • @Thomasvincent. Common rookie mistake. You can do something like `x1, x2, x3 = subs` to unpack, or store it into a dictionary with named keys, or why not just access it as `subs[0], subs[1], ...`, which is much more general and easy to use? – Mad Physicist Sep 07 '22 at 19:13
  • @MadPhysicist Yeah you're right my bad I just thought that putting them in variables for future use would be better but the 2nd method works fine. – Thomas vincent Sep 07 '22 at 19:15
  • 1
    @Thomasvincent. Everyone thinks that when they're starting out :) You should try it both ways and see which one is easier for you. Any way that passes all your requirements is correct. As you do this more, things like maintainability and extensibility become more and more important, but you don't have to worry about them when you're just starting out. – Mad Physicist Sep 07 '22 at 19:17
  • @Mad Physicist Thank you for the kind advice :) – Thomas vincent Sep 07 '22 at 19:20
  • 1
    @Mortz. Updated. Enjoy – Mad Physicist Sep 07 '22 at 19:43
  • Really nicely executed and explained. This is how SO used to be. – dawg Sep 07 '22 at 20:25
  • @dawg. I generally try to keep a certain level of professionalism in my answers, but I'm a grouchy old man in my comments and downvotes. Just the way the founding fathers intended :) – Mad Physicist Sep 07 '22 at 21:05
0

For three sub lists sublists = [[], [], []]:

  • First iteration place the next three items in sublists[0] , then sublists[1] then sublists[2]
  • Second iteration place the next three items in sublists[2] , then sublists[1] then sublists[0]
  • Third iteration switch the order again
  • continue flipping the order every three items till iteration is done

To make it generic for n sublist:

  • sublists = [[] for _ in range(n)]
  • get n items and put them in sublists[0], ..., sublist[n-1], sublist[n]
  • get next n items and put them in sublists[n], sublist[n-1],..., sublist[0]
  • repeat
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
wwii
  • 23,232
  • 7
  • 37
  • 77
  • You basically restated the question, which is not the same as providing an answer. – Mad Physicist Sep 07 '22 at 18:51
  • Ahh well since the question wasn't asking about any specific code error/malfunction/unexpected result I thought they were asking for an algorithmic type answer. It didn't dawn on me that they might want someone to just write them some code to solve their problem. – wwii Sep 07 '22 at 18:58
  • @wii. Even if that's not what they wanted, the question already explained the algorithm quite clearly. – Mad Physicist Sep 07 '22 at 18:59
  • Apparently it wasn't clear to the OP as they didn't try to turn that *algorithm* into a solution. – wwii Sep 07 '22 at 19:00
  • OK, I can buy that. – Mad Physicist Sep 07 '22 at 19:01