Nested loop

Discussion in 'Python' started by viewcharts, Nov 29, 2005.

  1. viewcharts

    viewcharts Guest

    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])
     
    viewcharts, Nov 29, 2005
    #1
    1. Advertising

  2. viewcharts

    Juho Schultz Guest

    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])
     
    Juho Schultz, Nov 30, 2005
    #2
    1. Advertising

  3. viewcharts

    Guest

    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.
     
    , Nov 30, 2005
    #3
  4. viewcharts

    Steve Holden Guest

    wrote:
    > 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.
    >

    The result of the readlines() function *is* a list, which is precisely
    why it's been used up:

    >>> f = file("mail.py")
    >>> type(f.readlines())

    <type 'list'>
    >>>


    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
    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Nov 30, 2005
    #4
  5. Steve Holden wrote:
    > wrote:
    >> 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.

    ....
    > 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
     
    Scott David Daniels, Nov 30, 2005
    #5
  6. On 30 Nov 2005 00:37:43 -0800, wrote:

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

    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
     
    Bengt Richter, Nov 30, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,145
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    659
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    431
  4. Ultrus
    Replies:
    3
    Views:
    383
    Stefan Behnel
    Jul 9, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page