Multiple variable control in for loops. Doable in Python?

Eduardo O. Padoan eduardo.padoan at gmail.com
Fri Jul 18 15:38:59 EDT 2008


On Fri, Jul 18, 2008 at 4:21 PM, mark floyd <emfloyd2 at gmail.com> wrote:
> I'm new to Python and have been doing work converting a few apps from Perl
> to Python.  I can not figure out the comparable Python structures for
> multi-variable for loop control.
>
> Examples:
>
> # In Perl
> for($i = 0, j = 0; $i < I_MAX && $j < J_MAX; $i+=5, $j += 10)
> {
>    ..... do something
> }
>
> // In Java
> class test {
>      public static void main(String[] args){
>           int i = 0;
>           int j = 1;
>           for(i=1, j = 0; i<11 && j < 10; i++, j++){
>                System.out.println("I is: " + i);
>                System.out.println("J is: " + j);
>           }
>      }
> }
>
>
> // In C
> #include <stdio.h>
> int main()
> {
>   int j = 0;
>   int k = 0;
>
>   for(j = 0, k = 0; j < 5 && k < 10; j++, k++) {
>     printf("J = %d\n", j);
>     printf("k = %d\n", k);
>     }
>
>   return 0;
>   }
>
> I spent a good part of yesterday looking for a way to handle this style for
> loop in Python and haven't been able to find an appropriate way to handle
> this control style.  We have this style for loop all over the place and not
> being able to find a similar structure in Python could be a problem.  Any
> pointers to a Python equivalent structure would be much appreciated
>
> - Mark
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Ops, sorry, sent only to Mark.

Here is the asnwer again:

for i, j in zip(range(0, I_MAX, 5), range(0, J_MAX, 10)):
   do_stuff(...)

-- 
 Eduardo de Oliveira Padoan
http://www.petitiononline.com/veto2008/petition.html
http://djangopeople.net/edcrypt/
http://whoisi.com/p/514
http://pinax.hotcluboffrance.com/profiles/edcrypt/



More information about the Python-list mailing list