Results 1 to 5 of 5

Thread: [Python] For/while loops on lists

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    [Python] For/while loops on lists

    I forgot bringing my book with me at work today...grrr...


    But here is what I need to do. I have 2 lists. One list has been made by reading in line by line a file. Then I have an other lists with words like this:

    [code]
    link_types = ["acro",
    "also",
    "award",
    ....
    "stat".
    "trans",
    "who",][/list]

    And I need to go through every line that doesn't start with a " " and check if one of those words are on the differnt lines. And it has to be fast.

    So I was guessing something like this would work:

    Code:
    print "\nChecking the link types:"
    line_number
    for line in lines:
        if line[0] == " ":
            found_it = 0
            for types in link_types:
                if types in line:
                    found_it = 1
    
            if found_it == 0:
                print line

    But here it checks for every word in the list, even if it has all ready found it. If would be much faster if the inner loop was a while loop. But how would I know when it came to the end of the link_types list?

    So what I want is something like this:
    Code:
    print "\nChecking the link types:"
    line_number = 0
    for line in lines:
        line_number = line_number + 1
        if line[0] == " ":
            found_it = 0
            while types in link_types and found_it != 0:
                if types in line:
                    found_it = 1
    
            if found_it == 0:
                print "\nLine: " + line_number + line

    But I am getting an error on the "while" line. Can't I use while on a list?

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: [Python] For/while loops on lists

    This may help if you haven't seen it yet...

    http://www.python.org/

    this is there, too
    VB Code:
    1. while_stmt      ::=     "while" expression ":" suite
    2.         ["else" ":" suite]

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: [Python] For/while loops on lists

    Well, yeah that is the syntax of a normal while loop, but it has nothing to do with traversing a list. So it is kind of unusefull.

    I am starting to think that it is not possible to traverse through a list using a while loop....


    ØØ

  4. #4
    Hyperactive Member VBD's Avatar
    Join Date
    Apr 2001
    Location
    The Place Above The Place Below Heavin
    Posts
    278

    Re: [Python] For/while loops on lists

    Code:
    for line in lines:
        line_number = line_number + 1
        if line[0] == " ":
            found_it = 0
            cindex = 0
            while cindex < len(linked_types) and found_it != 0:
                if linked_types[cindex] in line:
                    found_it = 1
                cindex=cindex+1
    A while loop is not designed to iterate through a list, while a for loop is. This is because a while loop is designed to continue going until certain conditions are met. Thus, you must do things manually. You could also do:
    Code:
    for line in lines:
        line_number = line_number + 1
        if line[0] == " ":
            found_it = 0
            cindex = 0
            lt2=linked_types[:]
            while len(lt2) and found_it != 0:
                types=lt2.pop()
                if types in line:
                    found_it = 1
                cindex=cindex+1
    Still, the best way of all is:
    Code:
    print "\nChecking the link types:"
    line_number
    for line in lines:
        if line[0] == " ":
            found_it = 0
            for types in link_types:
                if types in line:
                    found_it = 1
                    break
            if found_it == 0:
                print line
    When iterating through a list, use a For loop. Its designed for it. Just use the break statement to break out of it when you so desire.
    Hello

  5. #5

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: [Python] For/while loops on lists

    Well, the break is what I am using at the moment. But it looks ugly, at least in 2 nested for loops, then you have to have 2 break, and an if to test if it breaked on the inner one, something like:

    Code:
    for files_comp in files_and_lines:
         for line_comp in files_comp:
             if line_comp == line:
                  found = 1
                  break
    
         if found == 1:
              break
    
    
    if found == 0:
         print "Line: " + line_number
         print line


    And you are saying that while loops are designed to go on to a condition is met. I know that of course, but I was hoping there was a condition like EOF (End of file) or something for list, since it was so easy to iterate the list with a for loop, but so ugly when you have to use breaks.


    But since it doesn't look like that is possible, then I guess I just have to use breaks for list. Grrr....well thanks for the answer.


    ØØ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width