Faster 'if char in string' test

Harald Massa cpl.19.ghum at spamgourmet.com
Thu Jun 24 03:49:56 EDT 2004


Kamilche,

have you tried module sets?

tmp_validchars = string.ascii_letters + string.digits + \
               "!@#$%^&*()`~-_=+[{]}\\|;:\'\",<.>/?\t "
_validchars=sets.Set(tmp_validchars)

...

the test becomes:

testset=sets.Set(s)
if testset.difference(_validchars):
    raise Exception("Invalid character found!")


it may be even faster...


best wishes

Harald



More information about the Python-list mailing list