我想知道如何自动化我的嵌套以循环

发布于 2025-02-13 20:52:15 字数 323 浏览 1 评论 0原文

我想知道如何使嵌套循环自动化: 例如,如果x = 2,我将拥有:

for i in project['tasks']:
     for j in i['tasks']: 
          whatever...

如果x = 3,我将拥有:

for i in project['tasks']:
     for j in i['tasks']:
          for m in j['tasks']:
                whatever...

等...请自动化这个! 提前致谢。

I would like to know how can I automate my nested for loops :
for example if x = 2, i'll have this :

for i in project['tasks']:
     for j in i['tasks']: 
          whatever...

if x = 3, i'll have this :

for i in project['tasks']:
     for j in i['tasks']:
          for m in j['tasks']:
                whatever...

etc... how can automate this please !
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2025-02-20 20:52:15

也许您可以使用递归电话,类似的话:

def myfor(x):
    x -= 1
    for i in project['tasks']:
        if x > 0:
            myfor(x)
        # whatever
        print (x)

x = 3
myfor(x)

Maybe you could use recursive call, something like this:

def myfor(x):
    x -= 1
    for i in project['tasks']:
        if x > 0:
            myfor(x)
        # whatever
        print (x)

x = 3
myfor(x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文