Pythonic way to determine if one char of many in a string

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Feb 21 03:47:33 EST 2009


En Sat, 21 Feb 2009 01:14:02 -0200, odeits <odeits at gmail.com> escribió:
> On Feb 15, 11:31 pm, odeits <ode... at gmail.com> wrote:

>> It seems what you are actually testing for is if the intersection of
>> the two sets is not empty where the first set is the characters in
>> your word and the second set is the characters in your defined string.
>
> To expand on what I was saying I thought i should provide a code
> snippet:
>
> WORD = 'g' * 100
> WORD2 = 'g' * 50 + 'U'
> VOWELS = 'aeiouAEIOU'
> BIGWORD = 'g' * 10000 + 'U'
>
> def set_test(vowels, word):
>
>     vowels = set( iter(vowels))
>     letters = set( iter(word) )
>
>     if letters & vowels:
>         return True
>     else:
>         return False
>
> with python 2.5 I got 1.30 usec/pass against the BIGWORD

You could make it slightly faster by removing the iter() call: letters =  
set(word)
And (if vowels are really constant) you could pre-build the vowels set.

-- 
Gabriel Genellina




More information about the Python-list mailing list