简体   繁体   English

使用 arrays 进行动态迭代。 可能吗? 如果是的话怎么办?

[英]Dynamic Iteration with arrays. Is it possible? if yes how?

What I am trying to achieve is a couple of nested loops, where the number of nests will be dynamic.我想要实现的是几个嵌套循环,其中嵌套的数量是动态的。 So if for example there is 2 nested loops needed for the program then it will go因此,例如,如果程序需要 2 个嵌套循环,那么它将 go

for i in abc:
   for j in def:
       codes

So, if there is a need for 3 nested loops that is set by multiple conditions within the program, then the loop will be dynamically set, and that's what I don't know how to do or if it can be done.因此,如果程序中需要由多个条件设置的 3 个嵌套循环,那么循环将被动态设置,这就是我不知道该怎么做或是否可以完成的事情。 So I want it to be automatically do所以我希望它自动完成

 for i in abc:
       for j in def:
           for k in ggh:
               codes

So you can see I manually wrote the third nest, but can it be done automatically?所以你可以看到我手动写了第三个嵌套,但是可以自动完成吗? WITH PYTHON...带 PYTHON...

You can use a recursive function:您可以使用递归function:

l = [1, 2, 3, 4, [1, 2, 3, 4, [1, 2, 3], [1, 2]], [1, 2], [1, 2, 3, 4]]

def recursive_iter(list_):
    for i in list_:
        if isinstance(i, (list, tuple)):
            recursive_iter(i)
        else:
            print(i)

recursive_iter(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM