thanks, guys.
On Thu, Aug 14, 2008 at 7:35 AM, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
> 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
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
>> stateme
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(")",
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.
--
http://mail.python.org/mailman/listinfo/python-list
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" a
On Thu, 14 Aug 2008 01:54:55 +, Steven D'Aprano wrote:
> 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_
On Wed, 13 Aug 2008 22:50:29 +, 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
On Aug 14, 8:50 am, Wojtek Walczak
<[EMAIL PROTECTED]> 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
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 abo
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 an
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(")", "").repla
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
--
http://mail.python.o
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 t
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 bette
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("(", ""
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("/",
> "").rep
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, x
17 matches
Mail list logo