Running simultaneuos "FOR" loops

Duncan Booth duncan.booth at invalid.invalid
Tue Apr 23 04:59:37 EDT 2013


inshu chauhan <insideshoes at gmail.com> wrote:

> This statement is giving me the following error
> 
> Statement:
> for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
> pixel_count.iterkeys())):
> 
> Error:
> Traceback (most recent call last):
>   File "C:\Users\inshu\Desktop\Training_segs_trial2.py", line 170, in
><module>
>     access_segments(segimage, data)
>   File "C:\Users\inshu\Desktop\Training_segs_trial2.py", line 147, in
> access_segments
>     for p, k, j in zip(sorted(segments.iterkeys(),
>     class_count.iterkeys(), 
> pixel_count.iterkeys())):
> TypeError: 'dictionary-keyiterator' object is not callable
> 

The second argument to `sorted()` is a comparison or key function, if you 
want to sort all three key lists you need to sort them separately. Try:

for p, k, j in zip(sorted(segments),
                   sorted(class_count),
                   sorted(pixel_count)):

also you don't need to call the `iterkeys()` method as you need them all to 
sort and just treating the dict as a sequence will do the right thing.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list