-6
$\begingroup$

General problem: Using the elements of some list of length $m* n$, create a list with $m$ sub-lists, each of length $n$.

In my case, $m= 10 > n=3$.


The final output should be a list ("lis1") containing ten lists (the iterations of "nulis") with three elements each. Also, my current algorithm should leave "lis0" empty. The error is returned right at the last step. For some reason whenever it tries to take the last three (depth) elements of the original list the error below is returned.


From overload:

I am parsing some CSV files (four columns - one with a list of $3$ numbers, three with one number each about the respective lists) and am having some trouble with the last step: recombining the terms in the list-columns into $3$-element-long lists (the other columns are done).

The solution/explanation would be ideal, but I'll take an alternative approach if you've got one.

I can't remember some of the others, but I had tried using "for j in list: while (j == list[list.index(j)+1]) or (j == list[list.index(j)-1]): newlist.append(j)" but got memory errors. Solution for that would be sweet though not as pertinent because it would only work for a special case of what I'm looking into (one which works because of an inadequacy in another script I was writing). In the current version, I have tried to 'break' (concludes a step early but there's an overlooked error of the same type), 'continue', and 'pass', to no avail.


edited script into a function which gives the same error, feel free to give the special case (i.e. any solution to the problem outlined above with $m=10$, $n=3$)

def lispart(lis,length,depth):
    l0 = list(lis)
    l1 = []
    if ((len(l0)%length) != 0) or ((len(l0)%depth) != 0):
        return "Parity Error: length and/or depth do not correspond to list"
    else:
        for i in range(int(len(l0)/depth)):
            for j in range(depth):
                nulis = [l0[j] for j in range(depth)]
                l1.append(nulis)
                for k in nulis:
                    if k in l0:
                        l0.remove(l0[l0.index(k)])
print(lispart([i for i in range(30)], 10, 3))

I think the problem is due to there not being enough elements left to finish making new lists with. Ultimately, when you run it as I have it written out the last non-empty "lis0" printed statement is a list with just one number, namely the last element of the original list (which, fyi, had thirty elements). The last "lis1" print had $9$ elements, and the next (last "nulis") element is correctly formulated but I can't tell if it gets appended because there winds up being an error before anything else is printed.

Traceback (most recent call last):
  File "script.py", line X, in <module>
    nulis = [lis0[j] for j in range(3)]
  File "script.py", line X, in <listcomp>
    nulis = [lis0[j] for j in range(3)]
IndexError: list index out of range

Any help would be gorgeous, folks!

$\endgroup$
5
  • 2
    $\begingroup$ Unrelated to the actual question: Looking at the code, I wondered what len(10) should be, until I recognized that it was not the digit 1 but the letter l. It is a good idea to avoid such names (apart from the fact that this name isn't too descriptive anyway). $\endgroup$
    – celtschk
    Commented Jul 10, 2019 at 22:48
  • 3
    $\begingroup$ Is this a mathematical question? Looking at the answers given, this is definitely a Python question and not a mathematical question. $\endgroup$
    – Asaf Karagila
    Commented Jul 10, 2019 at 23:12
  • $\begingroup$ Sorry, I didn't realize you couldn't solve maths problems in python.. $\endgroup$
    – NomeFig
    Commented Jul 10, 2019 at 23:28
  • $\begingroup$ If you'd read the post you might have noted that I had already visited overflow and customized the problem for posting here. But yeah, make of life what you will. The error, as I understood was about counting elements which were not present in the list. $\endgroup$
    – NomeFig
    Commented Jul 10, 2019 at 23:36
  • $\begingroup$ I appreciate you not being sarcastic on your first day on the site. Nobody here owes you anything. You can optimize an ad for a used car for posting here, that won't make it on topic. $\endgroup$
    – Asaf Karagila
    Commented Jul 12, 2019 at 7:23

1 Answer 1

-1
$\begingroup$

I think what you want is

[list[i:i+n] for i in range(0,len(list),n)]

Where the inputs are the list named above as simply "list" and the length of each generated sub-list $n$. The value $m$ is redundant as the provided list has a length such that range(0,len(list),n) has $m$ values for the index $i$.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .