473,597 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 60668
Chris Brat <ch*******@gmai l.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*******@gmai l.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*******@gmai l.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************ **********@i3g2 000cwc.googlegr oups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
47805
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
2
12454
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 when validating the form inputs. Any suggestions? I've never delt with foreign languages before...any help greatly apprieciated!
6
21164
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 75 character) where the user has discribed the market for that domain. I need to loop through those records and find any feild that contains a "_" underscore or a "-" dash and replace those characters with a "/" slash. can anyone help me out with the VBA code to do this. thanks in advance...
5
1305
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 ID2 'd' #define ID3 'n' This is what I want in my output file:
3
9296
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 visible characters, it'd look something like this: "#Hello world" "@Hello word" "$Hello world" I'd like to replace the #, @, $ with invisible characters. These strings will be displayed in a windows datagrid cell.
3
2618
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 the .dll nicely in the GAC. How come it does not show up in my Add/Remove componenets list when I try to add it to the toolbar?
17
30649
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 don't know. If it's not possible, does anybody know of a VB.NET function (somebody must have coded this already) that will interpret strings containings those special characters, and handle them the same as in C#?
10
1830
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 string offsets is deprecated and will be removed in PHP 6 What do you think of that?
4
3022
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 than a-z, A-Z, 0-9 and the comma. I came across the following snippet in the online help but the output does not change at all:
0
7977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8276
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8044
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6705
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3936
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.