Re: Nested loop



viewcharts wrote:
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])
.



Relevant Pages

  • Re: Nested loop
    ... The problem is that when the inner loop is finished, it never goes back into the loop. ... for lookupSymbol in myfile: ... showme = lookupSymbol.split ...
    (comp.lang.python)
  • Re: Nested loop
    ... it never goes back into the loop. ... >> for refSymbol in symbols.readlines: ... >> for lookupSymbol in myfile.readlines: ... It would probably be more robust to check for blank lines and showme missing fields ...
    (comp.lang.python)
  • Re: Nested loop
    ... The problem is that when the inner loop is finished, it never goes back into the loop. ... it better be done outside of the outer loop. ...
    (comp.lang.python)
  • Re: LEN_TRIM performance issue
    ... to be in the inner loop. ... control parameters are stored as strings in a derived type. ... A long time ago in a Fortran 66 execution profiler ...
    (comp.lang.fortran)
  • Re: LEN_TRIM performance issue
    ... to be in the inner loop. ... time scales in the problem. ... control parameters are stored as strings in a derived type. ...
    (comp.lang.fortran)