Running simultaneuos "FOR" loops

Dave Angel davea at davea.name
Tue Apr 23 08:23:37 EDT 2013


On 04/23/2013 02:58 AM, inshu chauhan wrote:
> Yes Simultaneously means all three running at the same time, I looked up
> zip just now, but will it not disturb my dictionaries ?
> And yes the dictionaries have same number of keys.
>

More crucially, do all the dictionaries have the *same* keys?  If so, 
then all the zip logic is unnecessary, as the p, k, and j values will 
always be identical.

If they have the same keys, then do something like:

for key in sorted(segments):
     val1 = segments[key]
     val2 = class_count[key]
     val3 = pixel_count[key]
     ... do something with those values

If any of the keys is missing in one of the other dicts, you'll get an 
exception.  You might also want to do a check of the size of the 3 
dicts, just in case.


-- 
DaveA



More information about the Python-list mailing list