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

odeits odeits at gmail.com
Sat Feb 21 16:12:32 EST 2009


On Feb 21, 12:47 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Sat, 21 Feb 2009 01:14:02 -0200, odeits <ode... 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

set(word) = set{[word]} meaning a set with one element, the string
the call to iter makes it set of the letters making up the word.



More information about the Python-list mailing list