looping through two list simultenously

Discussion in 'Python' started by CSUIDL PROGRAMMEr, Oct 29, 2006.

  1. folks
    I have two lists

    i am trying to loop thorough them simultenously.

    Here is the code i am using

    f1 = os.popen('ls chatlog*.out')
    data1=f1.readlines()
    f1.close()

    data1=[x.strip() for x in data1]
    f1 = os.popen('ls chatlog*.txt')
    data=f1.readlines()
    f1.close()
    for eachline in data1 and line in data:

    filename='/root/Desktop/project/'+ eachline
    print filename
    outfile=open(filename,'r')
    filename1='/root/Desktop/project/' + line
    print filename1

    I get the error that line is not defined.
    Traceback (most recent call last):
    File "list.py", line 16, in ?
    for eachline in data1 and line in data:
    NameError: name 'line' is not defined

    Is there any efficient doing this
     
    CSUIDL PROGRAMMEr, Oct 29, 2006
    #1
    1. Advertising

  2. "CSUIDL PROGRAMMEr" <>:

    > folks I have two lists
    >
    > i am trying to loop thorough them simultenously.
    >
    > Is there any efficient doing this


    Try the built-in function zip.

    >>> zip(['a', 'b', 'c'], [1, 2, 3])

    [('a', 1), ('b', 2), ('c', 3)]

    --
    Björn Lindström <>
    Student of computational linguistics, Uppsala University, Sweden
     
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=, Oct 29, 2006
    #2
    1. Advertising

  3. > folks
    > I have two lists
    >
    > i am trying to loop thorough them simultenously.
    >
    > Here is the code i am using


    [...]

    > Is there any efficient doing this
    >


    Try the zip function:

    >>> list1 = [ 'a', 'b', 'c' ]
    >>> list2 = [ 'A', 'B', 'C' ]
    >>> for i, j in zip( list1, list2 ):

    .... print i, j
    ....
    a A
    b B
    c C
     
    Daniel Nogradi, Oct 29, 2006
    #3
  4. CSUIDL PROGRAMMEr

    Guest

    CSUIDL PROGRAMMEr wrote:
    > folks
    > I have two lists
    >
    > i am trying to loop thorough them simultenously.
    >
    > Here is the code i am using
    >
    > f1 = os.popen('ls chatlog*.out')

    You can replace this by
    py> import glob
    py> f1 = glob.glob('chatlog*.out')
    it will return a list of filenames in f1, so you can skip next two
    lines in your code
    > data1=f1.readlines()
    > f1.close()
    >

    If you use the glob this step can be skipped too, as glob returns the
    filenames, without any whitespace added
    > data1=[x.strip() for x in data1]


    > f1 = os.popen('ls chatlog*.txt')
    > data=f1.readlines()
    > f1.close()
    > for eachline in data1 and line in data:
    >
    > filename='/root/Desktop/project/'+ eachline
    > print filename
    > outfile=open(filename,'r')
    > filename1='/root/Desktop/project/' + line
    > print filename1
    >

    now you do the zip as suggested by the other people, but be aware that
    it only returns the tuples as long as the shortest list is, so it might
    be a good idea to add a test on equality of the number of .log and .txt
    files
    py> import glob
    py> f1 = glob.glob('chatlog*.out')
    py> f2 = glob.glob('chatlog*.txt')
    py> if len(f1) != len(f2):
    py> print "number of .out files doesn't match number of .txt files"
    py> f3 = zip(f1, f2)
    now f3 will be somethine like [('chatlog1.out',
    'chatlog1.txt'),('chatlog2.out', 'chatlog2.txt')], then you can
    continue with your iteration loops


    > I get the error that line is not defined.
    > Traceback (most recent call last):
    > File "list.py", line 16, in ?
    > for eachline in data1 and line in data:
    > NameError: name 'line' is not defined
    >
    > Is there any efficient doing this
     
    , Oct 29, 2006
    #4
  5. CSUIDL PROGRAMMEr

    jim-on-linux Guest

    On Sunday 29 October 2006 15:28, CSUIDL PROGRAMMEr
    wrote:
    > folks
    > I have two lists
    >
    > i am trying to loop thorough them
    > simultenously.
    >


    Try something like this.

    for eachline in data1:
    print eachline
    for line in data::
    print line

    You might also think about a while loop.

    jim-on-linux

    http://www.inqvista.com



    > Here is the code i am using
    >
    > f1 = os.popen('ls chatlog*.out')
    > data1=f1.readlines()
    > f1.close()
    >
    > data1=[x.strip() for x in data1]
    > f1 = os.popen('ls chatlog*.txt')
    > data=f1.readlines()
    > f1.close()
    > for eachline in data1 and line in data:
    >
    > filename='/root/Desktop/project/'+ eachline
    > print filename
    > outfile=open(filename,'r')
    > filename1='/root/Desktop/project/' + line
    > print filename1
    >
    > I get the error that line is not defined.
    > Traceback (most recent call last):
    > File "list.py", line 16, in ?
    > for eachline in data1 and line in data:
    > NameError: name 'line' is not defined
    >
    > Is there any efficient doing this
     
    jim-on-linux, Oct 30, 2006
    #5
  6. CSUIDL PROGRAMMEr

    Paul McGuire Guest

    "CSUIDL PROGRAMMEr" <> wrote in message
    news:...
    > folks
    > I have two lists
    >
    > i am trying to loop thorough them simultenously.
    >
    > Here is the code i am using
    >
    > f1 = os.popen('ls chatlog*.out')
    > data1=f1.readlines()
    > f1.close()
    >
    > data1=[x.strip() for x in data1]
    > f1 = os.popen('ls chatlog*.txt')
    > data=f1.readlines()
    > f1.close()
    > for eachline in data1 and line in data:


    Change:
    for eachline in data1 and line in data:

    to:
    for eachline, line in zip(data1,data):


    -- Paul
     
    Paul McGuire, Oct 30, 2006
    #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. saoirse_79

    looping through a list of lists.

    saoirse_79, Oct 8, 2003, in forum: Python
    Replies:
    0
    Views:
    261
    saoirse_79
    Oct 8, 2003
  2. Rob Hunter

    Re: looping through a list of lists.

    Rob Hunter, Oct 8, 2003, in forum: Python
    Replies:
    2
    Views:
    316
    anton muhin
    Oct 8, 2003
  3. Andy
    Replies:
    3
    Views:
    499
    James Kanze
    Jun 8, 2007
  4. Aaron
    Replies:
    2
    Views:
    577
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    304
Loading...

Share This Page