remove all elements in a list with a particular value

Steven Howe howe.steven at gmail.com
Thu May 17 17:56:35 EDT 2007


MRAB wrote:
> On May 16, 4:21 pm, Lisa <lisa.engb... at gmail.com> wrote:
>   
> I am reading in data from a text file.  I want to enter each value on
> the line into a list and retain the order of the elements.  The number
> of elements and spacing between them varies, but a typical line looks
> like:
>
> '   SRCPARAM   1 6.35e-07 15.00  340.00   1.10      3.0   '
>   
Using builtin functions:

    ax = '   SRCPARAM   1 6.35e-07 15.00  340.00   1.10      3.0   '
     >>> ax.replace('  ','')  # replace all double spaces with nothing
    ' SRCPARAM 1 6.35e-07 15.00340.00 1.103.0 '
     >>> ax.replace('  ','').strip() # strip leading/trailing white spaces
    'SRCPARAM 1 6.35e-07 15.00340.00 1.103.0'
     >>> ax.replace('  ','').strip().split(' ') # split string into a
    list, using remaining white space as key
    ['SRCPARAM', '1', '6.35e-07', '15.00340.00', '1.103.0']

    def getElements( str ):
        return str.replace( '  ', '' ).strip().split(' ')


sph

-- 
HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070517/2ae4e84a/attachment.html>


More information about the Python-list mailing list