There's got to be an easy way to do this

James Logajan JamesL at Lugoj.Com
Thu Jul 5 16:17:49 EDT 2001

Emile van Sebille wrote:
> I'm not sure how you mean slower, but I tested just now to see, and this is
> the fastest of the four.
[ Test results elided. ]

Not that it would affect the results much in this instance, but you really
should have benchmarked those algorithms on the same input string. And on
varying lengths too.

Now here is something you should add to your benchmarks:

from string import digits

def traditional(iters):
    for i in iters:
        result = ""
        for c in '(123)/456-7890':
            if c in digits:
                result = result + c

After you benchmark this, I'm sure you'll see that sometimes the "old" and
obvious ways are in fact good choices. And unlike some suggestions, the
above still works on 1.5.2....

More information about the Python-list mailing list