创建嵌套循环或函数

时间:2017-07-23 01:18:44

标签: python nested

是否可以创建一个嵌套在另一个中的循环链或函数链。使用if语句作为示例,我想链接...

if x == 0:
    print(x)

虽然像这样嵌套它们......

if x == 0:
    print(x)
    if x == 0:
       print(x)
       if x == 0:
           print(x)

如何使用自动流程执行此操作? 我想要这样做的代码是:

    oglist = ["a","b","c"]
combolist = ["lol"]
comboloop = True
combocounter = 3
recursions = {}
for counterx in range(1, combocounter):
    recursions["recursion" + str(counterx)] = False
def loopgen(counter2):
    if counter2 == 0:
            def loop():
                global comboloop
                global recursions
                if len(lists["list0"]) == 0:
                    comboloop = False
                    recursions["recursion1"] = True
                else:
                    lists["list1"] = lists["list0"][1:]
                    recursions["recursion1"] = False
                return
    elif counter2 != combocounter - 1:
            def loop():
                global recursions
                if len(lists["list" + str(counter2)]) == 0:
                    lists["list" + str(counter2 - 1)] = lists["list" + str(counter2 - 1)][1:]
                    recursions["recursion" + str(counter2)] = True
                    recursions["recursion" + str(counter2 + 1)] = True
                else:
                    lists["list" + str(counter2 + 1)] = lists["list" + str(counter2)][1:]
                    recursions["recursion" + str(counter2 + 1)] = False
                return
    else:
            def loop():
                global counter3
                global recursions
                if len(lists["list" + str(counter2)]) == 0:
                    lists["list" + str(counter2 - 1)] = lists["list" + str(counter2 - 1)][1:]
                    recursions["recursion" + str(counter2)] = True
                else:
                    combolist[counter3] = lists["list0"][0]
                    for x in range(1, combocounter):
                        combolist[counter3] = combolist[counter3] + lists["list" + str(x)][0]
                    combolist.append("lol")
                    lists["list" + str(counter2)] = lists["list" + str(counter2)][1:]
                    counter3 += 1
                    recursions["recursion" + str(counter2)] = False
                return
    return loop
lists = {}
for x in range(0, combocounter):
    lists["list" + str(x)] = ["lol"]
loops = {}
for counter1 in range(0, combocounter):
    loops["loop" + str(counter1)] = loopgen(counter1)
lists["list0"] = oglist
counter3 = 0
while comboloop == True:
    loops["loop0"]()
    while recursions["recursion1"] == False:
        loops["loop1"]()
        while recursions["recursion2"] == False:
            loops["loop2"]()
combolist.remove("lol")
print(combolist)

底部只有3个while循环,因为combocounter = 3 在此的未来版本中,combocounter将不是静态数字。

2 个答案:

答案 0 :(得分:0)

while循环:

while x == 0:
    print(x)

递归:

def f(x):
    if x == 0:
        print(x)
    f(x)

答案 1 :(得分:0)

我定义了另一个函数,它将在另一个while循环中复制while循环的创建...

def otherloop(counter4):
    while recursions["recursion" + str(counter4)] == False:
        loops["loop" + str(counter4)]()
        if counter4 != combocounter - 1:
            return otherloop(counter4 + 1)
    if counter4 != 1:
        return otherloop(counter4 - 1)
    return

然后当然我更换了

while recursions["recursion1"] == False:
    loops["loop1"]()
    while recursions["recursion2"] == False:
        loops["loop2"]()

otherloop(1)