remove all elements in a list with a particular value

L

Lisa

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 '

Why does the following not work:

line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
li = line.split(' ')
for j,i in enumerate(li):
if i == '':
li.remove(i)

After the original split I get:
['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '',
'340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']

And after the for loop I get:
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '', '', '',
'3.0', '', '', '']

It doesn't remove all of the empty elements. Is there a better way to
split the original string? Or a way to catch all of the empty
elements?

Thanks
 
L

Larry Bates

Lisa said:
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 '

Why does the following not work:

line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
li = line.split(' ')
for j,i in enumerate(li):
if i == '':
li.remove(i)

After the original split I get:
['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '',
'340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']

And after the for loop I get:
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '', '', '',
'3.0', '', '', '']

It doesn't remove all of the empty elements. Is there a better way to
split the original string? Or a way to catch all of the empty
elements?

Thanks

When you split on a space (' ') multiple spaces will return empty list
elements between them. Change to:

line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
li = line.split()

And you will get what you want:

['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']

-Larry
 
J

John Zenger

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 '

Why does the following not work:

line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
li = line.split(' ')
for j,i in enumerate(li):
if i == '':
li.remove(i)

After the original split I get:
['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '',
'340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']

And after the for loop I get:
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '', '', '',
'3.0', '', '', '']

It doesn't remove all of the empty elements. Is there a better way to
split the original string? Or a way to catch all of the empty
elements?

Thanks

As explained elsewhere, this is not the best way to do a split. But
if in the future you run into the problem of how to remove all
elements in a list with a particular value, here are two popular
approaches:
items = ['', '', '', 'SRCPARAM', '', '', '1', '6.35e-07', '15.00', '', '340.00', '', '', '1.10', '', '', '', '', '', '3.0', '', '', '']
print filter(lambda i: i != '', items) ['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']
print [x for x in items if x != '']
['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']
 
M

MRAB

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 '

Why does the following not work:

line = ' SRCPARAM 1 6.35e-07 15.00 340.00 1.10 3.0 '
li = line.split(' ')
for j,i in enumerate(li):
if i == '':
li.remove(i)
[snip]
What you're forgetting is that when you remove an item from a list all
the following items move down.

If you try this:

li = list('abc')
for i, j in enumerate(li):
print ", ".join("li[%d] is '%s'" % (p, q) for p, q in
enumerate(li))
print "Testing li[%d], which is '%s'" % (i, j)
if j == 'a':
print "Removing '%s' from li" % j
li.remove(j)

then you'll get:

li[0] is 'a', li[1] is 'b', li[2] is 'c'
Testing li[0], which is 'a'
Removing 'a' from li
li[0] is 'b', li[1] is 'c'
Testing li[1], which is 'c'

You can see that when 'enumerate' yielded li[0] 'b' was in li[1] and
when it yielded li[1] 'b' was in li[1]; it never saw 'b' because 'b'
moved!

Oops! Been there, done that... :)
 
S

Steven Howe

Steven said:
MRAB said:
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 '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
Made a mistake in the above code. Works well so long as there are no
double spaces in the initial string.
Try 2:
def compact( y ):
if y.find(' ') == -1:
return y
else:
y = y.replace( ' ', ' ' )
return compact( y )
['acicd', '1.345', 'aex', 'a;dae']

sph
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top