"W W" <[EMAIL PROTECTED]> wrote

Is there a faster way to strip punctuation from a string than this?

Yes

from string import ascii_letters

def strip_punctuation(mystring):
   newstring = ''
   for x in mystring:
       if x in ascii_letters:
           newstring += x

This is very slow since it creates a new string with each addition, it doesn't just append a character at the end as you might think! It would be faster to add the chars to a list then use string join() at the end. But see below...

       else:
           pass

The else/pass is redundant you could remove it.

However, it's much easier to do a replacement operation and especially using a regex of all the non ascii characters - or whatever you define to be punctuation...

That way you can do a single operation on the entire string which will all be coded in optimised C rather than
clunking through, letter by letter in Python.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to