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 7 60531
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
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
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
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.
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pikkel |
last post by:
i'm looking for a way to replace special characters with characters
without accents, cedilles, etc.
|
by: yearTiger2002 |
last post by:
Our company's back end database server does not handle french
accentuated characters well. We need to replace those characters with
english letters...
|
by: bobbie.matera |
last post by:
I have the system setup to import a DomainList.csv file into a table
called tblDmnLst. It contains a column called "NitchMarket" (datatype
= text...
|
by: Martijn |
last post by:
Hi,
I think I can best ask my question by example. I may be overlooking
something, but this is like what I have:
#define ID1 'i'
#define...
|
by: VM |
last post by:
If I have three exact strings composed of "Hello world", what 'invisible'
char could I add to the string so I can distinguish between them? With...
|
by: Zorpiedoman |
last post by:
Horay!
I have just put the finishing touches on a new User Control...
The" Jelly Button"
I created a setup program which runs fine. I see...
|
by: Carl Mercier |
last post by:
Hi,
Is it possible to use special characters like \n or \t in a VB.NET
string, just like in C#? My guess is NO, but maybe there's something I...
|
by: Chung Leong |
last post by:
Just saw a message in pl.comp.lang.php, which states that the following
message has appeared in the PHP CVS snapshot:
Usage of {} to access...
|
by: vvenk |
last post by:
Hello:
I have a string, "Testing_!@#$%^&*()". It may have single and double
quotations as well.
I would like to strip all chararcters others...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
| |