473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

remove all elements in a list with a particular value

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
6 2900
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
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
Great. Thanks for your help.

May 16 '07 #4
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
4
by: eksamor | last post by:
I have a simple linked list: struct element { struct element *next; int start; }; struct list { struct element *head;
12
by: Ju Hui | last post by:
I want to remove about 50000 elements from a list,which has 100000 elements. sample code like below: >>> a=range(10) >>> b=range(4) >>> for x in b: .... a.remove(x) .... >>> a
3
by: Beholder | last post by:
I hope that someone can help me with the following: Short background explenation: I have a shrfepoint page (newform.aspx) in a item list. On this page is a lookup column that displays a lookup...
0
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
4
by: =?Utf-8?B?emhhbmdscg==?= | last post by:
Hi, Following code does not work. (it will crash.)-- I think the reason is that I cannot modify list (call remove) in foreach. But I don't know what I can do. ...
10
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I wanted to go through each entry(?) of ArrayList and remove some particular entry. So I tried following but it throws exception at runtime: foreach (myEntry entry in myArrayList) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.