too many statically nested blocks python
Asked Answered
K

2

5

I'm trying to write more than 21 lists containing the same number of items to columns in a text file.

import random

a=[]
b=[]
....
q=[]


for i in range(200):
    a.append(random.gauss(10,0.1))
    b.append(random(20,0.5))
    ....
    q.append(random.gauss(50,0.2)

for aVal in a:
    for bVal in b:
        ....
        for qVal in q:
            print(aVal, "\t ", bVal, ", ", .... , qVal)

....

SystemError: too many statically nested blocks

How can I write each list to a column in a text file? e.g.

 0.892550 0.872493 0.206032 2.528080
 0.722350 0.303438 0.176304 2.436103
 0.875931 0.717765 0.144785 2.583095
 0.890831 0.411748 0.124370 2.540974
 0.764183 0.728080 0.128309 2.506590
 0.831232 0.545845 0.130100 2.517765
 0.754441 0.826074 0.208539 2.604585
 0.707450 0.367049 0.198868 2.503152
 0.736103 0.554441 0.097865 2.563324
 0.792837 0.808883 0.179527 2.526361
 0.719484 0.690258 0.215344 2.643266
 0.788252 0.824355 0.189198 2.455874
Kuntz answered 20/10, 2014 at 16:33 Comment(6)
What exactly is your goal? I'm not sure what you are getting at with the above code.Hulbig
What are you trying to do with e.g. random(10:1)? You should have a look at itertools.product (and maybe read the random docs while you're there...)Nagari
Did you have a question?Gellman
There's a limit on the number of nested blocks you can have in a piece of Python code. Reaching that limit should tell you that there's something WRONG with the code!Diatropism
Does the edit help>?Kuntz
All relevant questions: 1. https://mcmap.net/q/276850/-what-is-the-maximum-depth-level-of-nested-for-loops-in-python-3-duplicate 2. https://mcmap.net/q/273934/-why-does-python-have-a-limit-on-the-number-of-static-blocks-that-can-be-nestedCitizenry
R
4

"too many statically nested blocks", You will encounter this error when you nest blocks more than 20.

This is a design decision of python interpreter to restrict it to 20. Python uses a special stack called blockstack to execute code blocks, such as exception and loops. This stack size is limited to 20.

Though, the following code can be used in your context.

lst1 = [1, 11, 111]
lst2 = [2, 22, 222]
lst3 = [3, 33, 333]
lst4 = [4, 44, 444]
lst5 = [5, 55, 555]
lst6 = [6, 66, 666]


def lamlist(l1, l2, l3):
    funs = []
    for i in l1: 
        for j in l2: 
            for k in l3: 
                x = lambda i=i, j=j, k=k: (i,j,k)
                funs.append(x)
    return funs

#def lamlist(l1, l2, l3):
#    return [lambda i=i, j=j, k=k: (i, j, k) for i in l1 for j in l2 for k in l3] 


funlist = [lamlist(lst1, lst2, lst3), lamlist(lst4, lst5, lst6)]

for f1 in funlist[0]:
    a, b, c = f1()
    for f2 in funlist[1]:
        d, e, f = f2()
        print a, b, c, d, e, f

This code reduces your nesting by degree of 3.

Ricks answered 21/1, 2017 at 15:31 Comment(0)
B
2

Use zip, which aside from avoiding the error will print one line per group of values, not one set of lines for each value in the enclosing loop.

for aVal, bVal, ..., qVal in zip(a, b, ..., q):
    print(aVal, "\t ", bval, ", ", ..., qval)
Bourque answered 20/10, 2014 at 16:36 Comment(1)
If a, b, ..., q each have 10 items, this will print 10 lines, with the first line containing the first item of each list, etc.Bourque

© 2022 - 2024 — McMap. All rights reserved.