Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Better way to replace/remove characters in a list of strings. (http://www.velocityreviews.com/forums/t368435-better-way-to-replace-remove-characters-in-a-list-of-strings.html)

Chris Brat 09-04-2006 10:26 AM

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


Philipp Pagel 09-04-2006 10:45 AM

Re: Better way to replace/remove characters in a list of strings.
 
Chris Brat <chrisBrat@gmail.com> wrote:
> 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

George Sakkis 09-04-2006 11:02 AM

Re: Better way to replace/remove characters in a list of strings.
 
Philipp Pagel wrote:

> Chris Brat <chrisBrat@gmail.com> wrote:
> > 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


Chris Brat 09-04-2006 12:51 PM

Re: Better way to replace/remove characters in a list of strings.
 
Thanks, thats exactly what I was looking for - very neat.


George Sakkis wrote:
> Philipp Pagel wrote:
>
> > Chris Brat <chrisBrat@gmail.com> wrote:
> > > 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



Bruno Desthuilliers 09-04-2006 11:54 PM

Re: Better way to replace/remove characters in a list of strings.
 
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.

Chris Brat 09-05-2006 08:24 PM

Re: Better way to replace/remove characters in a list of strings.
 
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.



George Sakkis 09-05-2006 11:03 PM

Re: Better way to replace/remove characters in a list of strings.
 
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


Marc 'BlackJack' Rintsch 09-06-2006 06:54 AM

Re: Better way to replace/remove characters in a list of strings.
 
In <1157497380.382483.127700@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


All times are GMT. The time now is 11:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.