Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44401
| References | <CAFqGZRF4s_+9R4qDZTmFD7OWWMTn_YnhjKNMh8gY97ng5tyr9g@mail.gmail.com> <kldg7b$20b$1@ger.gmane.org> |
|---|---|
| Date | 2013-04-26 15:06 +0530 |
| Subject | Re: Nested For loop not running full |
| From | inshu chauhan <insideshoes@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1090.1366968997.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On Fri, Apr 26, 2013 at 2:39 PM, Peter Otten <__peter__@web.de> wrote:
> inshu chauhan 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.
>
> My crystal ball says that the 'for sy...' and 'for sx...' loops are running
> to completion, but you don't get the coordinates printed because you put
> them into the 'for l in f' loop which will only run once.
>
Is there any means by which I can run this 'For l in f' loop again and
again ?
>
> The quick and dirty fix is to replace
>
> f = open(...)
>
> in the code you are not showing with
>
> f == list(open(...))
>
f is just a text file(csv format).. so why list ??
> The reasonable thing to do is of course to move the preprocessing (e.g.
> csv-
> parsing) out of the sy and sx loops.
>
I did this but again then what I intend to do is not really happening, For
every pixel I read, I want to traverse the full file, so that the
information I am taking from pixel have to match in one of the line in the
file. Can this be done by modifying my code ? or something new has to be
devised ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Nested For loop not running full inshu chauhan <insideshoes@gmail.com> - 2013-04-26 15:06 +0530
csiph-web