469,268 Members | 920 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Better way to replace/remove characters in a list of strings.

Hi,

Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':

x = ["abbbb","123a","nnnnas"]

for i, v in enumerate(x) :
x[i] = v.replace("a","b")
This works, but I'd like to know peoples opinions.

Thanks
Chris

Sep 4 '06 #1
7 60339
Chris Brat <ch*******@gmail.comwrote:
Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':
x = map(lambda foo: foo.replace('a', 'b'), x)

cu
Philipp

--
Dr. Philipp Pagel Tel. +49-8161-71 2131
Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
Technical University of Munich
http://mips.gsf.de/staff/pagel
Sep 4 '06 #2
Philipp Pagel wrote:
Chris Brat <ch*******@gmail.comwrote:
Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':

x = map(lambda foo: foo.replace('a', 'b'), x)
Or more pythonically:

x = [s.replace('a', 'b') for s in x]

George

Sep 4 '06 #3
Thanks, thats exactly what I was looking for - very neat.
George Sakkis wrote:
Philipp Pagel wrote:
Chris Brat <ch*******@gmail.comwrote:
Is there a better way to replace/remove characters (specifically ' and
" characters in my case, but it could be anything) in strings in a
list, than this example to replace 'a' with 'b':
x = map(lambda foo: foo.replace('a', 'b'), x)

Or more pythonically:

x = [s.replace('a', 'b') for s in x]

George
Sep 4 '06 #4
Chris Brat a écrit :
Thanks, thats exactly what I was looking for - very neat.
Just note that both solutions rebind the name to a newly constructed
list instead of modifying the original list in place. This is usually
the RightThing(tm), but sometimes one wants an in-place modification.
Sep 4 '06 #5
Hi

Wouldn't this only cause problems with large lists - for once off
scripts with small lists it doesn't seem like a big issue to me.

Regards,
Chris

Bruno Desthuilliers wrote:
Chris Brat a écrit :
Thanks, thats exactly what I was looking for - very neat.
Just note that both solutions rebind the name to a newly constructed
list instead of modifying the original list in place. This is usually
the RightThing(tm), but sometimes one wants an in-place modification.
Sep 5 '06 #6
Chris Brat wrote:
Hi

Wouldn't this only cause problems with large lists - for once off
scripts with small lists it doesn't seem like a big issue to me.

Regards,
Chris

Bruno Desthuilliers wrote:
Chris Brat a écrit :
Thanks, thats exactly what I was looking for - very neat.
>
Just note that both solutions rebind the name to a newly constructed
list instead of modifying the original list in place. This is usually
the RightThing(tm), but sometimes one wants an in-place modification.

The extra memory to allocate the new list is usually a minor issue; the
important one is correctness, if the original list is referenced by
more than one names. Check the following almost identical-looking
cases:
1)
>>x = ["abbbb","123a","nnnnas"]
y = x
x = [s.replace('a', 'b') for s in x] # rebind to new list
y is x
False

2)
>>x = ["abbbb","123a","nnnnas"]
y = x
x[:] = [s.replace('a', 'b') for s in x] # in place modification
y is x
True

Neither case is always "the correct"; correctness depends on the
problem at hand, so you should know the difference and decide between
rebinding and mutation accordingly.

George

Sep 5 '06 #7
In <11**********************@i3g2000cwc.googlegroups. com>, George Sakkis
wrote:
Chris Brat wrote:
>Wouldn't this only cause problems with large lists - for once off
scripts with small lists it doesn't seem like a big issue to me.

The extra memory to allocate the new list is usually a minor issue; the
important one is correctness, if the original list is referenced by
more than one names.
It's not the allocation of the new list itself that might be an issue but
the content, which will be copied in this case, before the old list and
its content is freed. If you have 200 MiB worth of strings in the list
and change all 'u's to 'x's with

large_list = [item.replace('u', 'x') for item in large_list]

another list with 200 MiB strings will be created.

Ciao,
Marc 'BlackJack' Rintsch
Sep 6 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by yearTiger2002 | last post: by
5 posts views Thread by Martijn | last post: by
10 posts views Thread by Chung Leong | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.