473,671 Members | 2,370 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 60669
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
47811
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
2
12456
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
21167
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
1306
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
9304
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
2620
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
30655
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
1833
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
3024
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
8472
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
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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
6222
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
5690
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
4221
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...
1
2806
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.