473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replace Several Items

I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?
Aug 13 '08 #1
15 1558
gjhames:
What's the better way to do it?
Better is a relative term. If with better you mean "faster" (in some
circumstances), then the translate method is your friend, as you can
see its second argument are the chars to be removed. As first argument
you can use something like:
"".join(map(chr, xrange(256)))
If your strings are unicode you will need something different (a dict
with Null values for the key chars you want to remove).

Bye,
bearophile
Aug 13 '08 #2
I tend to use the re module like so :

import re
my_string = re.sub('[\-,./]','',my_string)
I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?
Aug 13 '08 #3
Dnia Wed, 13 Aug 2008 09:39:53 -0700 (PDT), gjhames napisa³(a):
I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?
The regular expression is probably the best way to do it,
but if you really want to use replace, you can also use
the replace method in loop:
>>somestr = "Qwe.Asd/Zxc()Poi-Lkj"
for i in '-./()':
.... somestr = somestr.replace(i, '')
....
>>somestr
'QweAsdZxcPoiLkj'
>>>

Next step would be to define your own replacing function:

def my_replace(mystr, mychars, myrepl):
"""Replace every character from 'mychars' string with 'myrepl' string
in 'mystr' string.

Example:

my_replace('Qwe.Asd/Zxc(', './(', 'XY') -'QweXYAsdXYZxcXY'"""

for i in mychars:
mystr = mystr.replace(i, myrepl)

return mystr
--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 13 '08 #4
Wojtek Walczak wrote:
>I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?

The regular expression is probably the best way to do it,
but if you really want to use replace, you can also use
the replace method in loop:
suggested exercise: benchmark re.sub with literal replacement, re.sub
with callback (lambda m: ""), repeated replace, and repeated use of the form

if ch in my_string:
my_string = my_string.replace(ch, "")

on representative data.

</F>

Aug 13 '08 #5
Fredrik Lundh:
suggested exercise: benchmark re.sub with literal replacement, re.sub
with callback (lambda m: ""), repeated replace, and repeated use of the form
....
on representative data.
Please, add the translate() solution too I have suggested :-)

Bye,
bearophile
Aug 13 '08 #6
On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote:
I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?
--
http://mail.python.org/mailman/listinfo/python-list

The maketrans interface is a bit clunky, but this is what
string.translate is best at:

>>import string
>>'-./other'.translate( string.maketrans( '', '' ), '-./' )
'other'

It'd be interesting to see where it falls in the benchmarks, though.

It's worth noting that the interface for translate is quite different
for unicode strings.
--
John Krukoff <jk******@ltgc.com>
Land Title Guarantee Company

Aug 13 '08 #7
Dnia Wed, 13 Aug 2008 23:31:42 +0200, Fredrik Lundh napisa³(a):
>>I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?

The regular expression is probably the best way to do it,
but if you really want to use replace, you can also use
the replace method in loop:

suggested exercise: benchmark re.sub with literal replacement, re.sub
with callback (lambda m: ""), repeated replace, and repeated use of the form

if ch in my_string:
my_string = my_string.replace(ch, "")

on representative data.
I don't have to, I can anticipate the results. I mentioned above
that using re is the best approach, but if one really wants to use
replace() multiple times (which will be slow, of course), it can
be done a bit cleaner than with str.replace().replace().replace()...

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 13 '08 #8
Wojtek Walczak wrote:
>suggested exercise: benchmark re.sub with literal replacement, re.sub
with callback (lambda m: ""), repeated replace, and repeated use of the form

if ch in my_string:
my_string = my_string.replace(ch, "")

on representative data.

I don't have to, I can anticipate the results.
Chances are that you're wrong.

</F>

Aug 13 '08 #9
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
>> if ch in my_string:
my_string = my_string.replace(ch, "")

on representative data.

I don't have to, I can anticipate the results.

Chances are that you're wrong.
At the moment my average is about 0.75 of mistake per
post on comp.lang.python (please, bare with me ;-)).
I strongly believe that the statement I made above won't
make this number rise.

:)
--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 13 '08 #10
On Aug 14, 8:50 am, Wojtek Walczak
<gmin...@nie.ma.takiego.adresu.w.sieci.plwrote:
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
> if ch in my_string:
my_string = my_string.replace(ch, "")
>on representative data.
I don't have to, I can anticipate the results.
Chances are that you're wrong.

At the moment my average is about 0.75 of mistake per
post on comp.lang.python (please, bare with me ;-)).
I strongly believe that the statement I made above won't
make this number rise.
Meta-mistake: Playing poker with the effbot. If he says diffidently
that he'll raise you a dime, it means he's holding five aces :-)

Clue: The effbot was the original author of the modern (Python 1.6?)
version of the re module.

HTH,
John
Aug 14 '08 #11
On Wed, 13 Aug 2008 22:50:29 +0000, Wojtek Walczak wrote:
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
>>> if ch in my_string:
my_string = my_string.replace(ch, "")

on representative data.

I don't have to, I can anticipate the results.

Chances are that you're wrong.

At the moment my average is about 0.75 of mistake per post on
comp.lang.python (please, bare with me ;-)). I strongly believe that the
statement I made above won't make this number rise.

:)


Okay, is this going to be one of those things where, no matter what the
benchmarks show, you say "I was right, I *did* anticipate the results. I
just anticipated them correctly/incorrectly."?

If so, you get an A+ in pedantry and F- in usefulness *wink*

In full knowledge that Python is relatively hard to guess what is fast
compared to what is slow, I'll make my guess of fastest to slowest:

1. repeated replace
2. repeated use of the form
"if ch in my_string: my_string = my_string.replace(ch, "")
3. re.sub with literal replacement
4. re.sub with callback (lambda m: "")
Results to follow.
--
Steven
Aug 14 '08 #12
Steven D'Aprano wrote:
While I'm gratified that my prediction was so close to the results I
found, I welcome any suggestions to better/faster/more efficient code.
more things to try:
code tweaks:

- Factor out the creation of the regular expression from the tests:
"escape" and "compile" are relatively expensive, and neither throw-away
code (using the RE function forms) nor production code will end up doing
them both for each string.

- Same w. the translation table for "translate"

- Use Unicode strings instead of byte strings (we're moving towards 3.0,
after all).

test data variations:

- Try dropping the number of actual replacements and see what happens --
if you're escaping user-provided data (e.g. HTML), for example, it's not
that unlikely that you end up doing only a few replacements for each
string you're processing, or no replacements at all.

- Also try shorter and longer strings ("human-sized" data is often
provided in shorter chunks than 216 characters per string; the typical
size and distribution depends on your actual application, of course).

Unicode will affect translate more than the others; the last two will
most likely affect in-replace instead (that approach gets faster the
shorter the strings are, and the fewer calls to replace that you
actually end up doing).

Finally, if you want the sub-lambda form to look better, try inserting a
character before or after each special character using a template string
or a lambda (e.g. a backslash).

</F>

Aug 14 '08 #13
John Machin wrote:
Clue: The effbot was the original author of the modern (Python 1.6?)
version of the re module.
And the author of the "in" and "replace" implementations in Python 2.5.

</F>

Aug 14 '08 #14
On 2008-08-13 23:54, John Krukoff wrote:
On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote:
>I wish to replace several characters in my string to only one.
Example, "-", "." and "/" to nothing ""
I did like that:
my_string = my_string.replace("-", "").replace(".", "").replace("/",
"").replace(")", "").replace("(", "")

But I think it's a ugly way.

What's the better way to do it?
--
http://mail.python.org/mailman/listinfo/python-list


The maketrans interface is a bit clunky, but this is what
string.translate is best at:

>>import string
>>>'-./other'.translate( string.maketrans( '', '' ), '-./' )
'other'

It'd be interesting to see where it falls in the benchmarks, though.

It's worth noting that the interface for translate is quite different
for unicode strings.
Right. Unicode .translate() uses a dictionary for defining the
mapping.

Another approach is to use the re module:
>>import re
re.sub('[-./()]', '', '-./other')
'other'

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Aug 14 2008)
>>Python/Zope Consulting and Support ... http://www.egenix.com/
mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
__________________________________________________ ______________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
Aug 14 '08 #15
On 14 Aug 2008 01:54:55 GMT, Steven D'Aprano wrote:
>>>I don't have to, I can anticipate the results.

Chances are that you're wrong.

At the moment my average is about 0.75 of mistake per post on
comp.lang.python (please, bare with me ;-)). I strongly believe that the
statement I made above won't make this number rise.
Okay, is this going to be one of those things where, no matter what the
benchmarks show, you say "I was right, I *did* anticipate the results. I
just anticipated them correctly/incorrectly."?
Don't count on it. I never really cared about being right or wrong
and I am not one of those guys who are trying to prove something
that actually makes no difference at all. Nice tests, though :)
--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 14 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: andrea.gavana | last post by:
Hello NG, probably this is a basic question, but I'm going crazy... I am unable to find an answer. Suppose that I have a file (that I called "Errors.txt") which contains these lines: MULTIPLY...
7
by: ajikoe | last post by:
Hello, I would like to replace string with different values, For example : source = 'kode1 bla bla kode1 bla kode1' I have a list with each member will replace each of kode1. L = So the new...
1
by: Luke Dalessandro | last post by:
I have an application where there is a primary XML data file. I'll use the following as an example: <data> <item id="a"> <name>A</name> <price>$10</price> </item> <item id="b">...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: **Developer** | last post by:
Been experimenting with menu items merging and it seems to me that all I need is Replace and Remove If the item is not already there Add, Merge and Replace all seem to add it. If it is already...
1
by: mimenko | last post by:
Hello, I'd want to show and hide the same icons (pictures) on a web page that contains several icons. Some are identical, some are different (for ex : 3 items "A", 4 items "B", 2 items "C"). Is...
4
by: ds4ff1z | last post by:
Hello, i'm looking to find and replace multiple characters in a text file (test1). I have a bunch of random numbers and i want to replace each number with a letter (such as replace a 7 with an f ...
1
by: Dinis Correia | last post by:
Hi all, How can I replace an inherited member name? I've built a class that inherits from KeyedCollection(Of ..., ...). I would like to replace base class Items property with Fields property. Is...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.