too many statically nested blocks python

后端 未结 2 1581
野趣味
野趣味 2021-01-14 02:35

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(20         


        
相关标签:
2条回答
  • 2021-01-14 02:59

    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)
    
    0 讨论(0)
  • 2021-01-14 03:03

    "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.

    0 讨论(0)
提交回复
热议问题