Loops Control with Python
Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1 ( 0 ...100) { IN: foreach my $s2 (@array) { if ($s1 == $s2) { next OUT; } else { last IN; } } } How can we implement that construct with Python? -- Edward WIJAYA SINGAPORE Institute For Infocomm Research - Disclaimer - This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
Wijaya Edward wrote: Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1 ( 0 ...100) { IN: foreach my $s2 (@array) { if ($s1 == $s2) { next OUT; } else { last IN; } } } How can we implement that construct with Python? Literally. for si in range(100 + 1): for s2 in some_array: if s1 == s2: break Same thing, but nicer. for si in range(100 + 1): if si in some_array: # Do something here. Cheers, Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
Wijaya Edward wrote: Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1 ( 0 ...100) { IN: foreach my $s2 (@array) { if ($s1 == $s2) { next OUT; } else { last IN; } } } How can we implement that construct with Python? If you are not willing to determine the problem the code is written to solve, you are doomed to working with Perl in Python. While I think Python is a far better language than Perl, I remain convinced that Perl is a better Perl than Python. Describe an actual problem, don't simply give an example from another language. If you don't know about break, continue, and for ... else, go study the Python language. -- --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
Wijaya Edward wrote: Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1 ( 0 ...100) { IN: foreach my $s2 (@array) { if ($s1 == $s2) { next OUT; } else { last IN; } } } How can we implement that construct with Python? Python does not use Labels. If you want to quit a single loop then look up the Python break statement. If you want to exit deeply nested execution then Python has exceptions. this maybe new to a Perl programmer so please take time to understand Python exceptions. There follows a function that you can call from an interactive session to explore one type of use for exceptions that is rather like your use of Perl labels shown. == class Outer(Exception): pass class Inner(Exception): pass def skip_loops(y1 = -1, y2 = -1, y3 = -1): ''' Shows how to skip parts of nested loops in Python''' try: for x0 in range(3): try: for x1 in range(3): for x2 in range(3): if x2 == y2: raise Inner if x2 == y3: break print (x0,x1,x2) if x1 == y1: raise Outer print (x0,x1) except Inner: print Raised exception Inner print (x0,) except Outer: print Raised exception Outer == skip_loops(y1=2) (0, 0, 0) (0, 0, 1) (0, 0, 2) (0, 0) (0, 1, 0) (0, 1, 1) (0, 1, 2) (0, 1) (0, 2, 0) (0, 2, 1) (0, 2, 2) Raised exception Outer skip_loops(y2=2) (0, 0, 0) (0, 0, 1) Raised exception Inner (0,) (1, 0, 0) (1, 0, 1) Raised exception Inner (1,) (2, 0, 0) (2, 0, 1) Raised exception Inner (2,) - Paddy. P.S. Welcome to Python! -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
Paddy wrote: Wijaya Edward wrote: Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1 ( 0 ...100) { IN: foreach my $s2 (@array) { if ($s1 == $s2) { next OUT; } else { last IN; } } } How can we implement that construct with Python? Python does not use Labels. If you want to quit a single loop then look up the Python break statement. If you want to exit deeply nested execution then Python has exceptions. this maybe new to a Perl programmer so please take time to understand Python exceptions. There follows a function that you can call from an interactive session to explore one type of use for exceptions that is rather like your use of Perl labels shown. == class Outer(Exception): pass class Inner(Exception): pass def skip_loops(y1 = -1, y2 = -1, y3 = -1): ''' Shows how to skip parts of nested loops in Python''' try: for x0 in range(3): try: for x1 in range(3): for x2 in range(3): if x2 == y2: raise Inner if x2 == y3: break print (x0,x1,x2) if x1 == y1: raise Outer print (x0,x1) except Inner: print Raised exception Inner print (x0,) except Outer: print Raised exception Outer == skip_loops(y1=2) (0, 0, 0) (0, 0, 1) (0, 0, 2) (0, 0) (0, 1, 0) (0, 1, 1) (0, 1, 2) (0, 1) (0, 2, 0) (0, 2, 1) (0, 2, 2) Raised exception Outer skip_loops(y2=2) (0, 0, 0) (0, 0, 1) Raised exception Inner (0,) (1, 0, 0) (1, 0, 1) Raised exception Inner (1,) (2, 0, 0) (2, 0, 1) Raised exception Inner (2,) - Paddy. P.S. Welcome to Python! How about a thread on GOTOs ? ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
hg wrote: Paddy wrote: P.S. Welcome to Python! How about a thread on GOTOs ? ;-) I'm trying to be nice on c.l.p. - Mind you, I do have that rant as part of my blog: http://paddy3118.blogspot.com/2006/03/whats-wrong-with-perl.html ;-) - Paddy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loops Control with Python
hg wrote: How about a thread on GOTOs ? ;-) A thread? No need! There's a module: http://entrian.com/goto/ ;-) STeVe -- http://mail.python.org/mailman/listinfo/python-list