Need help removing list elements.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nuffnough@gmail.com

    #1

    Need help removing list elements.

    This is python 2.4.3 on WinXP under PythonWin.

    I have a config file with many blank lines and many other lines that I
    don't need.

    read the file in, splitlines to make a list, then run a loop that
    looks like this:



    config_file = open("lines.txt ", "rb")
    returned_lines = config_file.rea d().splitlines( )

    i = len(returned_li nes)


    for i in range(i):
    if returned_lines[i].find("Value") == -1:
    if returned_lines[i].find("Name") == -1:
    print "read in this useless line ..."
    print returned_lines[i]
    print "Removing line ..."
    returned_lines[i] = ""




    This blanks out all the lines I don't want. I did originally try 'del
    returned_lines[i]' but I got list index out of range, so I made a loop
    to delete the empty elements.

    for i in range(i):
    if returned_lines[i] == "":

    del returned_lines[i]



    But this gives me "IndexError : list out of range

    After much experimentation and dumping of the list, I have figured out
    that it doesn't like removing multiple empty elements in a row. In
    other words, if there are 4 empty lines, it will remove one of them,
    and seems to behave as though that was one element instead of 3. if I
    make i = i - *number of groups of empty elements* it works without an
    error, but leaves many empty elements behind.

    Obviously I can iterate over it time and again, but that isn't how the
    world should work.

    Is this something obvious that I am doing wrong, or something more
    complicated?

    Any help will be gratefully appreciated!

  • jwelby

    #2
    Re: Need help removing list elements.

    This looks like a job for list comprehensions:
    [color=blue][color=green][color=darkred]
    >>> returned_lines= ['Name: John, Value: 12','We don't want this one.','Name: Eric, Value: 24']
    >>> [x for x in returned_lines if ('Name' in x and 'Value' in x)][/color][/color][/color]
    ['Name: John, Value: 12', 'Name: Eric, Value: 24']

    List comprehensions are great. If you are not familiar with them, check
    out the Python documentation. Once you get started with them, you won't
    look back.

    Comment

    • jwelby

      #3
      Re: Need help removing list elements.

      Ooops!

      Looking at your example a bit closer, change the 'and' in the list
      comprehension I posted to 'or', and it should do what you want.

      Comment

      • Max Erickson

        #4
        Re: Need help removing list elements.

        nuffnough@gmail .com wrote in
        news:1146316090 .540227.278230@ j73g2000cwa.goo glegroups.com:
        [color=blue]
        > But this gives me "IndexError : list out of range[/color]

        You are making the list shorter as you are iterating. By the time your
        index is at the end of the original list, it isn't that long any more.
        Creating a new list and appending the elements you want to keep avoids
        the problem. Or you can just use a list comprehension(u ntested):

        returned_lines=[line for line in open("lines.txt ", 'rb')
        if line != ""]

        or just

        returned_lines=[line for line in open("lines.txt ") if line]

        max


        Comment

        • John Machin

          #5
          Re: Need help removing list elements.

          On 30/04/2006 12:22 AM, Max Erickson wrote:[color=blue]
          > nuffnough@gmail .com wrote in
          > news:1146316090 .540227.278230@ j73g2000cwa.goo glegroups.com:
          >[color=green]
          >> But this gives me "IndexError : list out of range[/color]
          >
          > You are making the list shorter as you are iterating. By the time your
          > index is at the end of the original list, it isn't that long any more.[/color]

          If you are hell-bent on conditionally deleting items from a list in
          situ, you need to do it backwards:

          for i in xrange(len(alis t)-1, -1, -1):
          if not_interested( alist[i]):
          del alist[i]
          [color=blue]
          > Creating a new list and appending the elements you want to keep avoids
          > the problem. Or you can just use a list comprehension(u ntested):
          >
          > returned_lines=[line for line in open("lines.txt ", 'rb')[/color]

          Call me crazy, but I wouldn't open the file in BINARY mode :-)
          [color=blue]
          > if line != ""]
          >
          > or just
          >
          > returned_lines=[line for line in open("lines.txt ") if line][/color]

          For a modicum of extra effort, the condition "if line.strip()" throws
          away lines containing only whitespace.

          However I don't see the point of creating a list of lines, then throwing
          out only *some* of the uninteresting ones. IMHO the OP might be better
          advised to read the file one line at a time, ignoring
          blank/empty/comment lines, then *validate* the remainder. Hint: with the
          semi-squished-list approach, you can't report the original line number
          of any erroneous line without extra effort.

          The OP might be even better advised to (read the source of, use) an
          existing config file parser module.

          Hope some of this helps,
          John

          Comment

          • nuffnough@gmail.com

            #6
            Re: Need help removing list elements.

            Thanks to all those who responded. It has all helped immensely. :-)

            I didn't know about list comprehensions before I started.

            very warm regards to all.

            Comment

            Working...