Running simultaneuos "FOR" loops

Gary Herron gary.herron at islandtraining.com
Tue Apr 23 02:53:32 EDT 2013


On 04/22/2013 11:40 PM, inshu chauhan wrote:
> i have to implement the below line in one of my code:
>
> for  p in sorted(segments.iterkeys()) and for k in 
> sorted(class_count.iterkeys()) and for j in 
> sorted(pixel_count.iterkeys()):
>
> Its giving me a syntax error which is obvious, but how can I make all 
> three for loop run simultaneously or any other way to do 
> this simultaneous work ???
>
>

Be clearer about the problem please.

Do you wish to produce a loop that:
   On pass 1, each of p,k, and t hold the first item of their respective 
lists, and
   on pass 2, each of p,k, and t hold the second item of their 
respective lists, and
   so on
until one (or all) lists run out?

If that is what you want, then check out the zip builtin function. But 
also consider this:  Do you care what happens if one list runs out 
before the others?



Or is it something else you want?  Perhaps nested loops?
   for  p in sorted(segments.iterkeys()):
       for k in sorted(class_count.iterkeys()):
           for j in sorted(pixel_count.iterkeys()):
              # This will be run with all possible combinations of p,k, 
and t.

Gary Herron




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130422/c006e042/attachment.html>


More information about the Python-list mailing list