
August 13th, 2008, 05:45 PM
| | | 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? | 
August 13th, 2008, 06:35 PM
| | | Re: Replace Several Items
gjhames: Quote: |
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 | 
August 13th, 2008, 06:45 PM
| | | Re: Replace Several Items
I tend to use the re module like so :
import re
my_string = re.sub('[\-,./]','',my_string) Quote:
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?
| | 
August 13th, 2008, 10:05 PM
| | | Re: Replace Several Items
Dnia Wed, 13 Aug 2008 09:39:53 -0700 (PDT), gjhames napisa³(a): Quote:
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: Quote: Quote: Quote:
>>somestr = "Qwe.Asd/Zxc()Poi-Lkj"
>>for i in '-./()':
| | | .... somestr = somestr.replace(i, '')
.... '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/ | 
August 13th, 2008, 10:35 PM
| | | Re: Replace Several Items
Wojtek Walczak wrote: Quote: Quote:
>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> | 
August 13th, 2008, 10:55 PM
| | | Re: Replace Several Items
Fredrik Lundh: Quote:
suggested exercise: benchmark re.sub with literal replacement, re.sub
with callback (lambda m: ""), repeated replace, and repeated use of the form
| .... Please, add the translate() solution too I have suggested :-)
Bye,
bearophile | 
August 13th, 2008, 10:55 PM
| | | Re: Replace Several Items
On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote: Quote:
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 Quote: Quote: Quote: |
>>'-./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 <jkrukoff@ltgc.com>
Land Title Guarantee Company | 
August 13th, 2008, 11:25 PM
| | | Re: Replace Several Items
Dnia Wed, 13 Aug 2008 23:31:42 +0200, Fredrik Lundh napisa³(a): Quote: Quote: Quote:
>>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/ | 
August 13th, 2008, 11:45 PM
| | | Re: Replace Several Items
Wojtek Walczak wrote: Quote: Quote:
>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> | 
August 13th, 2008, 11:55 PM
| | | Re: Replace Several Items
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a): Quote: Quote: Quote:
>> 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/ | 
August 14th, 2008, 01:35 AM
| | | Re: Replace Several Items
On Aug 14, 8:50 am, Wojtek Walczak
<gmin...@nie.ma.takiego.adresu.w.sieci.plwrote: Quote:
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
> Quote: Quote:
> if ch in my_string:
> my_string = my_string.replace(ch, "")
| | >> Quote: Quote: |
I don't have to, I can anticipate the results.
| | > Quote: |
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 | 
August 14th, 2008, 02:55 AM
| | | Re: Replace Several Items
On Wed, 13 Aug 2008 22:50:29 +0000, Wojtek Walczak wrote: Quote:
Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
> Quote: Quote:
>>> 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 | 
August 14th, 2008, 07:35 AM
| | | Re: Replace Several Items
Steven D'Aprano wrote: Quote:
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> | 
August 14th, 2008, 07:35 AM
| | | Re: Replace Several Items
John Machin wrote: Quote:
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> | 
August 14th, 2008, 12:15 PM
| | | Re: Replace Several Items
On 2008-08-13 23:54, John Krukoff wrote: Quote:
On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote: Quote:
>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 Quote: Quote: |
>>>'-./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: Quote: Quote: Quote:
>>import re
>>re.sub('[-./()]', '', '-./other')
| | | 'other'
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Aug 14 2008) __________________________________________________ ______________________
:::: 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 | 
August 14th, 2008, 12:35 PM
| | | Re: Replace Several Items
On 14 Aug 2008 01:54:55 GMT, Steven D'Aprano wrote: Quote: Quote: Quote:
>>>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.
| | Quote:
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/ |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|