Nested loop

V

viewcharts

I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?


for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])
 
J

Juho Schultz

viewcharts said:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?


for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])

When the inner loop is finished for the 1st time,
myfile has been read. So next time you start to the
loop, myfile.readlines() returns an empty list.

Something like this should be better:

lookupSymList = myfile.readlines()
for refSymbol in symbols.readlines():
for lookupSymbol in lookupSymList:
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])
 
B

bonono

viewcharts said:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?


for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])
As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.
 
S

Steve Holden

viewcharts said:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?


for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])

As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.
The result of the readlines() function *is* a list, which is precisely
why it's been used up:

A second call to readlines just gets an empty list, since there are no
more lines left to be read.
Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.
The solution, as already proposed, is to bind the list of lines to a
nanme so it can be reused.

regards
Steve
 
S

Scott David Daniels

Steve said:
viewcharts said:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?

for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])

As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.
....
> The solution, as already proposed, is to bind the list of lines to a
nanme so it can be reused.

regards
Steve

Or you could read each on the fly, and rewind the inner:

for refSymbol in symbols:
for lookupSymbol in myfile:
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip() + " " + showme[10])
myfile.seek(0)

This is probably more what you wanted, but Steve's suggestion will run
much faster.

--Scott David Daniels
(e-mail address removed)
 
B

Bengt Richter

I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?


for refSymbol in symbols.readlines():
for lookupSymbol in myfile.readlines():
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])
As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.
Yes, and unless there is an ordering requirement that can't be ignored or achieved
by sorting afterwards, symbols seems like it could be a set. E.g., (untested)

refSymbolSet = set(refSymbol.strip() for refSymbol in symbols)
for showme in (lookupSymbol.split('\t') for lookupSymbol in myfile):
if showme[3] in refSymbolSet:
priceNew.write(showme[3]+" "+showme[10])

It would probably be more robust to check for blank lines and showme missing fields
and symbol duplicates also.

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top