嵌套循环限制? [英] Nested loop limit?

查看:88
本文介绍了嵌套循环限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来进行一些可靠性计算,

需要几个嵌套的for循环。但是,我相信随着

模型变得越来越复杂,所需的for循环次数将增加
。 Python对嵌套for循环的数量有限制吗?

谢谢。

I am writing a program to do some reliability calculations that
require several nested for-loops. However, I believe that as the
models become more complex, the number of required for-loops will
increase. Does Python have a limit on the number of nested for-loops?
Thanks.

推荐答案

chad写道:
chad wrote:
我正在编写一个程序来进行一些可靠性计算,这需要几个嵌套的for循环。但是,我相信随着
模型变得越来越复杂,所需的for循环次数将会增加。 Python是否对嵌套for循环的数量有限制?
I am writing a program to do some reliability calculations that
require several nested for-loops. However, I believe that as the
models become more complex, the number of required for-loops will
increase. Does Python have a limit on the number of nested for-loops?


for n in range (100):
.... exec''\ n''。join([(''''* i)+''for i%s in range(2):''%i for我在

范围(n)])

+''\ n''+''''* n +''pass \ n''

....

回溯(最近一次调用最后一次):

文件"< stdin>",第2行,在?

SystemError:太多静态嵌套块打印n
for n in range(100): .... exec ''\n''.join([('' '' * i) + ''for i%s in range(2):'' % i for i in
range(n)])
+ ''\n'' + '' '' * n + ''pass\n''
....
Traceback (most recent call last):
File "<stdin>", line 2, in ?
SystemError: too many statically nested blocks print n



21


是。 :-)


-Peter


21

Yes. :-)

-Peter


>>>>> "乍得" == chad< ch ********* @ hp.com>写道:


Chad>我正在写一个程序来做一些可靠性计算

Chad>这需要几个嵌套的for循环。但是,我相信

Chad>随着模型变得越来越复杂,

Chad>所需的for循环将增加。 Python是否有限制

Chad>关于嵌套for循环的数量?谢谢。


不知道(可能不是),但如果您甚至需要考虑这样的事情,

您可能想重新考虑设计。嵌套for循环50左右
由于缩进,
甚至不适合屏幕。


通常通过引入函数可以避免过度嵌套。


-

Ville Vainio http://tinyurl.com/2prnb
>>>>> "Chad" == chad <ch*********@hp.com> writes:

Chad> I am writing a program to do some reliability calculations
Chad> that require several nested for-loops. However, I believe
Chad> that as the models become more complex, the number of
Chad> required for-loops will increase. Does Python have a limit
Chad> on the number of nested for-loops? Thanks.

No idea (probably not), but if you even need to think of such a thing,
you might want to reconsider the design. 50 or so nested for loops
wouldn''t even fit on the screen due to indentation.

Typically excessive nesting can be avoided by introduding a function.

--
Ville Vainio http://tinyurl.com/2prnb


彼得已经回答了你的具体问题。问题,但

我以为我会提出一个建议。如果你发现自己有很多嵌套循环的b $ b,你可能需要仔细看看你的类/数据结构。通常我会发现,如果我发现我在循环很多,我可能

没有优化我的数据存储或对象结构

好​​吧。使用列表推导可以有效地消除许多循环,但是您的数据必须在

数字(或对象)列表中构建,以使它们运行良好。函数

作用于对象列表也可以消除许多

嵌套循环。自己持有列表的对象列表

的对象可以让你创建复杂的层次结构,而不需要很多嵌套列表。

只是一些可能有益的想法。


问候,

Larry Bates

Syscon,Inc。

" chad" < CH ********* @ hp.com>在消息中写道

news:fd ************************** @ posting.google.c om ...
Peter already answered your "specific" question, but
I thought I''d make a suggestion. If you find yourself
with LOTS of nested loops, you probably need to take
a closer look at your class/data structure. Often I
find that if I find that I''m looping a lot, I probably
haven''t optimized my data storage or object structures
well. Using list comprehensions can effectively eliminate
many loops, but your data must be structured in lists of
numbers (or objects) for them to work well. Functions
that act on lists of objects can also eliminate many
nested loops. Lists of objects that themselves hold lists
of objects can allow you to create complex hierarchies that
don''t require lots of nested lists.

Just some thoughts that might be of benefit.

Regards,
Larry Bates
Syscon, Inc.
"chad" <ch*********@hp.com> wrote in message
news:fd**************************@posting.google.c om...
我正在编写一个程序来进行一些可靠性计算,这需要几个嵌套的for循环。但是,我相信随着
模型变得越来越复杂,所需的for循环次数将会增加。 Python对嵌套for循环的数量有限制吗?
谢谢。
I am writing a program to do some reliability calculations that
require several nested for-loops. However, I believe that as the
models become more complex, the number of required for-loops will
increase. Does Python have a limit on the number of nested for-loops?
Thanks.



这篇关于嵌套循环限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆