-1

Working on an assignment, which in one of the parts requires me to split the list into "x" chunks of lists but I don't necessarily want to use the elements inside the list to do so as I have seen many examples that do this. For example, given the list [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], I want to split this list into 3 even parts ([[1,2,3,4,5],[6,7,8,9,10],[11,12,14,15]]).

Most of the examples that I have seen on here tend to divide the whole list by "x" rather than making "x" groups of it which end up giving me a list like [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]].

EDIT:

I've figured out how to get the desired output in the first part of this question that I was looking for but upon looking at my assignment further, I realized the lists that need to be made have to go up in increments of lst rather than having to put a value in which would generate a list in that range. For example if I want to print out a grouped list like [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]] I don't want to have have to set lst to lst = list(range(1,lst+1)) and then when I'm assigning values to my function, I want to be able to put 5 in place of 15 to get the same result.

def test(num, lst, start = 1):
    lst = list(range(start,lst+1))
    avg = len(lst) / float(num)
    out = []
    last = 0.0

    while last < len(lst):
        out.append(lst[int(last):int(last + avg)])
        last += avg

    return out

print (test(3,15))
Cireo
  • 4,197
  • 1
  • 19
  • 24
snk
  • 91
  • 1
  • 8
  • 2
    Does this answer your question? [Splitting a list into N parts of approximately equal length](https://stackoverflow.com/questions/2130016/splitting-a-list-into-n-parts-of-approximately-equal-length) – ggorlen Nov 25 '19 at 20:01
  • Or possibly this: https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks – gen_Eric Nov 25 '19 at 20:01
  • 1
    Simple arithmetic. If you want `x` groups from a list with `n` items, then you just need to calculate `n / x`, and then you can divide the whole list by that result. Given 5 groups of a list with 20 elements, you need to split every 4 items. – DetectivePikachu Nov 25 '19 at 20:02
  • Could you clarify these parts? *"without using the elements on the inside"* ... *"I don't necessarily want to use the elements inside the list to do so"*. – wjandrea Nov 25 '19 at 20:57
  • If the length of the list isn't a multiple of `rows`, where should the remainder go? – wjandrea Nov 25 '19 at 21:00
  • 1
    BTW that code output doesn't include 15. It looks like you just forgot to add 1: `list(range(1, lst+1))` – wjandrea Nov 25 '19 at 21:03
  • @wjandrea I realized the lists that need to be made have to go up in increments of "lst" rather than having to put a value in which would generate a list in that range. Any suggestions would be appreciated – snk Nov 25 '19 at 21:24

1 Answers1

0

Most of the examples that I have seen on here tend to divide the whole list by "x" rather than making "x" groups of it which end up giving me a list like [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]].

In your example, a list of 15 elements divided into chunks of 3 makes 5 groups. It's too bad you didn't want five groups instead of three, you would have been done already!

If only there was a way to divide 15 elements with different sized chunks such that you would have 3 groups =)

Hint:

3 x 5 = 15
5 x 3 = 15

tl;dr: If you believe that dividing a list into chunks of size "x" is different than making "y" groups, then you should take a minute to reconsider.

Cireo
  • 4,197
  • 1
  • 19
  • 24