Multiple variable control in for loops. Doable in Python?

mark floyd emfloyd2 at gmail.com
Fri Jul 18 21:21:49 CEST 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080718/57c58d14/attachment.html>


More information about the Python-list mailing list