Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44403
| References | <CAFqGZRF4s_+9R4qDZTmFD7OWWMTn_YnhjKNMh8gY97ng5tyr9g@mail.gmail.com> <kldg7b$20b$1@ger.gmane.org> <CAFqGZRGoh=hTG7PZgxNaaKgq8Z_QHyqVQZ+voe1z_KY+-ysjug@mail.gmail.com> |
|---|---|
| Date | 2013-04-26 19:45 +1000 |
| Subject | Re: Nested For loop not running full |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1092.1366969559.3114.python-list@python.org> (permalink) |
On Fri, Apr 26, 2013 at 7:36 PM, inshu chauhan <insideshoes@gmail.com> wrote:
>
> On Fri, Apr 26, 2013 at 2:39 PM, Peter Otten <__peter__@web.de> wrote:
>>
>> f = open(...)
>>
>> in the code you are not showing with
>>
>> f == list(open(...))
>
> f is just a text file(csv format).. so why list ??
(That should be =, not ==)
Instead of having an open file object, you would instead have a list
of the lines in the file. That can be iterated over more than once.
>> 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 ?
How large is the file? There are two easy solutions:
1) Open and close the file every time you touch a pixel
2) Open the file once, read it all into memory, and then iterate over
the in-memory copy every pixel
If your file is insanely large then the first option may be better,
but for anything less than five yottabytes, go with the second. (Okay,
I may be exaggerating slightly... let's say anything less than half
your RAM. So if you have 10YB of memory, then I wasn't exaggerating.)
That's why Peter suggested creating a list; you iterate over the list.
Another way to do it is to parse the file once and retain a more
efficient and useful structured form of the data... which is the other
thing Peter suggested ("move the preprocessing (e.g. csv-
parsing) out of the sy and sx loops").
So, yeah. Listen to Peter Otten, he knows what he's talking about :)
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Nested For loop not running full Chris Angelico <rosuav@gmail.com> - 2013-04-26 19:45 +1000
csiph-web