Remove Whitespace

John Machin sjmachin at lexicon.net
Fri Apr 14 02:33:36 EDT 2006


On 14/04/2006 12:51 PM, Felipe Almeida Lessa wrote:
> Em Sex, 2006-04-14 às 12:46 +1000, Steven D'Aprano escreveu:
>> Why would you want to call in the heavy sledgehammer of regular
>> expressions for cracking this peanut?
> 
> And put heavy on that!
> 
> $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
> 4 0'" 'str.replace(" ", "")'

Oi! The OP mentioned "whitespace" ...

> 100000 loops, best of 3: 3.07 usec per loop
> $ python2.4 -mtimeit -s "str = 'D c a V e r \" = d w o r d : 0 0 0 0 0 6
> 4 0'" '"".join(str.split())'
> 100000 loops, best of 3: 4.16 usec per loop
> $ python2.4 -mtimeit -s "from re import sub; str = 'D c a V e r \" = d w
> o r d : 0 0 0 0 0 6 4 0'" 'sub("\\s", "", str)'
> 10000 loops, best of 3: 23.6 usec per loop
> $ calc 23.6 / 3.07
>         ~7.68729641693811074919
> 
> I couldn't be expressed better:
> 
> "Some people, when confronted with a problem, think "I know, I'll use
> regular expressions." Now they have two problems."
>       — Jamie Zawinski, in comp.lang.emacs
> 


C:\junk>python -mtimeit -s "str = 23 * ' X'" "str.replace(' ', '')
100000 loops, best of 3: 3.65 usec per loop

C:\junk>python -mtimeit -s "str = 23 * ' X'" "str.replace(' ', 
'').replace('\t', '')
100000 loops, best of 3: 4.33 usec per loop

C:\junk>python -mtimeit -s "str = 23 * ' X'; cmap = ''.join(chr(k) for k 
in range(256)); delchars = ' \t'" "str.translate(cmap, delchars)"
1000000 loops, best of 3: 0.883 usec per loop

0.883 / 3.65 -> 0.242

Some people, in Gadarene flight from regular expressions, don't read far 
enough in the Library Reference Manual :-)



More information about the Python-list mailing list