Nested loop

bonono at gmail.com bonono at gmail.com
Wed Nov 30 03:37:43 EST 2005


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])
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.




More information about the Python-list mailing list