Nested For loop not running full

Jens Thoms Toerring jt at toerring.de
Fri Apr 26 05:54:37 EDT 2013


inshu chauhan <insideshoes at gmail.com> wrote:
> I have this part of my code where I am trying to traverse over an image by
> running a for loop for both x and y co-ordinate axis. But the loop is
> terminating by just reading first pixel. Can think of a reason why this is
> happening ?

> The code is:
> for sy in xrange(0, segimage.height):
>     for sx in xrange(0, segimage.width):
>             if segimage[sy,sx] == (0.0, 0.0, 0.0):
>                 continue
>             else:
>                 seg_color = segimage[sy,sx]
>                 blue = int(seg_color[0])
>                 green = int(seg_color[1])
>                 red = int(seg_color[2])
>                 reg_num = blue + 256 * green + 65536 * red
>                 for l in f:
>                     sp = l.split(",")
>                     if len(sp) == 14:
>                         print sy, sx  # for checking which pixel its
> reading currently
>                         print reg_num, sp[0]  # for checking whats
> happening
>                         if reg_num == int(sp[0].strip()):
>                             print reg_num, sp[0].strip() # for checking
> whats happening
>                             classification = int(sp[13].strip())


> The inside "for loop" is for reading a csv format file from which I am
> extracting some information.

Are you sure that the loop is only run once? In that case the most
likely thing is that the image consists of only a single pixel
(or all except the first one are black, then it might also look
as if the loop would be run only once;-)

But what looks strange is the innermost loop. You never tell what
exactly 'f' is but I would tend to assume that it is a file object
for your CSV file, which you opened somewhere before. And now you
read it in completely when dealing with the very first pixel of
your image. Afterwards, when dealing with the other pixels of the
image, there's nothing left to be read in, so the inner loop won't
be run again, making it appear as if the outer loops would only be
run once.

If my assumptions are correct and you want to read in the file
again and again for each pixel then you should either open it
again and again for each pixel or, probably better, reset the
file object so that it "points" back to the start of the file
before the start of the innermost loop, using the seek() method
- a simple "f.seek(0)" should do the job (assuming that this is
a normal file, i.e. one that can be "rewound" and not e.g. a re-
directed pipe).

An even better solution (if you have enough memory) might be to
read in the whole file into a list and iterate over that instead
of the file itself. And better than that might be to build a
dictionary of values in the file that you can use later on, so
you don't have to run over the whole file again and again:

    d = { }
    for l in f :
        sp = split( l, ',' )
        if len( sp ) == 14 :
            d[ int( sp[ 0 ].strip( ) ) ] = int( sp[ 13 ].strip( ) )

Then you can later check directly if some color value (what
you have named 'reg_num') is in the file by using 

    if reg_num in d :

and the corresponding value from the file (what you assign
to 'classification') is simply the value of the dictionary
for the key given by 'reg_num'. i.e.

        classification = d[ reg_num ]

                        Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt at toerring.de
   \__________________________      http://toerring.de



More information about the Python-list mailing list