[Baypiggies] More dramatic material?

Andrew Dalke dalke at dalkescientific.com
Thu Feb 25 11:54:35 CET 2010


On Feb 24, 2010, at 11:35 PM, Glen Jarvis wrote:
> But so did the equivalent regular expression, yes?  I compared that the count gave the same results in both. 

Oh, absolutely. It's just that regular expressions have a clear path to being able to solve the problem while with the other Python string operations it's a lot trickier.

One solution is to make a translation table

>>> import string
>>> to_list = [" ",]*256
>>> to_list[ord("A"):ord("Z")+1] = string.ascii_lowercase
>>> to_list[ord("a"):ord("z")+1] = string.ascii_lowercase
>>> table = string.maketrans( "".join(chr(i) for i in range(256)), "".join(to_list))
>>> line = "The best and the worst there ever was."
>>> line.translate(table)
'the best and the worst there ever was '
>>> (" " + line.translate(table) + " ").count(" the ")
2
>>> 

vs.

>>> re.findall(r"(^|\W)the(\W|$)", line, re.I)
[('', ' '), (' ', ' ')]
>>> len(re.findall(r"(^|\W)the(\W|$)", line, re.I))
2
>>> 


				Andrew
				dalke at dalkescientific.com




More information about the Baypiggies mailing list