Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: how many nested for can we utilize?

197 views
Skip to first unread message

Fredrik Lundh

unread,
Aug 17, 2008, 5:23:06 AM8/17/08
to pytho...@python.org
Patrol Sun wrote:

> when I use 20 for ,"SystemError: too many statically nested blocks"
> When I use 100 for ,"IndentationError: too many levels of indentation"
> How to handle these errors?

so why exactly are you trying to nest 20 or 100 for-in loops?

</F>

Nick Dumas

unread,
Aug 17, 2008, 10:20:45 AM8/17/08
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

A good quote I read (I can't remember who it was from, though) is "If
you need more than three levels of indentation, then something is
seriously wrong with your code." Possibly Guido himself? Anyway. If
you've got 100 levels of for, you're probably making things way harder
than they need to be.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkioM70ACgkQLMI5fndAv9jBhgCeM5hjNaAtlDosJq1DSZyPnBcL
7NYAoKKwnSdR20YDGXvjpAP8KUWfw/rl
=PajF
-----END PGP SIGNATURE-----

Fredrik Lundh

unread,
Aug 17, 2008, 10:39:26 AM8/17/08
to pytho...@python.org
Nick Dumas wrote:

> A good quote I read (I can't remember who it was from, though) is "If
> you need more than three levels of indentation, then something is
> seriously wrong with your code." Possibly Guido himself? Anyway. If
> you've got 100 levels of for, you're probably making things way harder
> than they need to be.

assuming 100 levels of for and 2 items in each sequence, you'll end up
with 1267650600228229401496703205376 iterations through the inner loop.
assuming each iteration takes a picosecond, it'll take approx 40
billion years to run the program.

</F>

Wojtek Walczak

unread,
Aug 17, 2008, 11:08:57 AM8/17/08
to

I guess that's exactly why the OP asks the question. He just wants
to start as soon as possible ;-)

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/

Fredrik Lundh

unread,
Aug 17, 2008, 11:17:06 AM8/17/08
to pytho...@python.org
Wojtek Walczak wrote:

>> it'll take approx 40 billion years to run the program.
>
> I guess that's exactly why the OP asks the question. He just wants
> to start as soon as possible ;-)

required reading:

"The Effects of Moore's Law and Slacking on Large Computations"
http://arxiv.org/abs/astro-ph/9912202

</F>

Paul Boddie

unread,
Aug 17, 2008, 12:13:21 PM8/17/08
to
On 17 Aug, 17:17, Fredrik Lundh <fred...@pythonware.com> wrote:
>
> required reading:
>
> "The Effects of Moore's Law and Slacking on Large Computations"
> http://arxiv.org/abs/astro-ph/9912202

Or maybe this...?

http://docs.python.org/tut/node6.html#SECTION006600000000000000000

And this:

http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.htm

Paul

Wojtek Walczak

unread,
Aug 17, 2008, 1:32:37 PM8/17/08
to

Kinda buddhist approach. Anyway, it might work out, unless the number
of for loops is increasing in time and in 18 months it may be - let's
say - 20 nested for loops more ;) Got to ask the OP.

Fredrik Lundh

unread,
Aug 17, 2008, 1:36:37 PM8/17/08
to pytho...@python.org
Paul Boddie wrote:

>> required reading:
>>
>> "The Effects of Moore's Law and Slacking on Large Computations"
>> http://arxiv.org/abs/astro-ph/9912202
>
> Or maybe this...?
>
> http://docs.python.org/tut/node6.html#SECTION006600000000000000000

and functions will solve the 40-billion year computation problem exactly
how?

</F>

Paul Boddie

unread,
Aug 17, 2008, 2:27:58 PM8/17/08
to
On 17 Aug, 19:36, Fredrik Lundh <fred...@pythonware.com> wrote:
>
> and functions will solve the 40-billion year computation problem exactly
> how?

I was thinking more about the need to nest "for" statements to a depth
of 20 levels, which I imagine only arises on a "just in case" basis
for the inquirer. In other words, the inquirer may have input that
could theoretically demand twenty levels of state, each level provided
by a single "for" statement, but in practice they might only need to
manage a few levels of state.

In any case, we're potentially seeing the use of the wrong tool for
the job, and I am speculating that functions would be the right tool,
but unless the inquirer is willing to show the code (which I haven't
seen in this thread yet), it's hard to speculate further.

Paul

Roel Schroeven

unread,
Aug 17, 2008, 3:19:37 PM8/17/08
to
Nick Dumas schreef:

> A good quote I read (I can't remember who it was from, though) is "If
> you need more than three levels of indentation, then something is
> seriously wrong with your code." Possibly Guido himself?

Linus Torvalds in the Linux kernel coding style document:

"[...] The answer to that is that if you need more than 3 levels of
indentation, you're screwed anyway, and should fix your program."

(http://lxr.linux.no/linux/Documentation/CodingStyle)

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven

Gabriel Genellina

unread,
Aug 18, 2008, 6:20:47 AM8/18/08
to pytho...@python.org
En Sun, 17 Aug 2008 21:57:46 -0300, Patrol Sun <sunp...@gmail.com> escribió:

> Of course We needn't 100 levels,but I use the exec function can concise the
> code. See the attachment.

Just a note on the attached code:

def isPro52Num(n):
s=[]
for i in range(1,7):
s.append(str(i*n))
for i in s[0]:
ll = len(string.join(string.split(s[0],i),''))
for j in range(1,6):
if ll!=len(string.join(string.split(s[j],i),'')):
return False
return True

The functions in the string module have been deprecated ages ago in favor of the corresponding string methods.
In this expression: len(string.join(string.split(s[0],i),'')) if you want to get "the number of digits in s[0] that are not i" I think it's way faster (and more clear) to do len(s[0])-s[0].count(i) instead of creating two lists just to throw them away.

--
Gabriel Genellina

Lie

unread,
Aug 18, 2008, 11:04:18 AM8/18/08
to

I think most (all?) algorithm that might use 20+ levels of for-in
loops could almost always be replaced with a recursive function. Or at
least they could be broken into smaller functions.

Reedick, Andrew

unread,
Aug 18, 2008, 11:18:52 AM8/18/08
to pytho...@python.org


Recursion is slow. There was a thread on permutations and I ran some timings on various methods of nesting loops. Also, I do remember hitting nested for limits very quickly. I think list comprehensions allowed for deeper nesting than straight for loops. Anyway, the thread contains various code and solutions that should help the OP.

http://mail.python.org/pipermail/python-list/2008-January/473787.html

Cookbook is relatively decent. 5 deep, 100 iterations:
list comprehension: 11.02
nested for loop : 13.16 +19%
cookbook : 18.85 +71%
recursion : 69.00 +526%
eval : 13.30 +20%


Start of thread: http://mail.python.org/pipermail/python-list/2008-January/473650.html

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623


0 new messages