By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

remove all elements in a list with a particular value

P: n/a
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

May 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Lisa 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 '

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
May 16 '07 #2

P: n/a
On May 16, 11:21 am, Lisa <lisa.engb...@gmail.comwrote:
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']

May 16 '07 #3

P: n/a
Great. Thanks for your help.

May 16 '07 #4

P: n/a
John Zenger wrote:
>>>>print [x for x in items if x != '']

['SRCPARAM', '1', '6.35e-07', '15.00', '340.00', '1.10', '3.0']
This can be shortened to

[x for x in items if x]

James
May 17 '07 #5

P: n/a
On May 16, 4:21 pm, Lisa <lisa.engb...@gmail.comwrote:
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... :-)

May 17 '07 #6

P: n/a
Steven Howe wrote:
MRAB wrote:
>On May 16, 4:21 pm, Lisa <lisa.engb...@gmail.com<mailto:lisa.engb...@gmail. comwrote:

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
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 )
>>ax = ' acicd 1.345 aex a;dae '
print compact( ax )
acicd 1.345 aex a;dae
>>print compact( ax ).strip().split()
['acicd', '1.345', 'aex', 'a;dae']

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

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

May 19 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.