Loops Control with Python

Jon Clements joncle at googlemail.com
Fri Oct 13 11:28:48 EDT 2006


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.




More information about the Python-list mailing list