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

odeits odeits at gmail.com
Sat Feb 21 19:53:25 EST 2009


On Feb 21, 2:24 pm, rdmur... at bitdance.com wrote:
> odeits <ode... at gmail.com> wrote:
> > On Feb 21, 12:47=A0am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > wrote:
> > > En Sat, 21 Feb 2009 01:14:02 -0200, odeits <ode... at gmail.com> escribi=F3:
>
> > > > On Feb 15, 11:31=A0pm, 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.
>
> > 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.
>
> Did you try it?
>
> Python 2.6.1 (r261:67515, Jan  7 2009, 17:09:13)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> set('abcd')
>
> set(['a', 'c', 'b', 'd'])
>
> --RDM

You are in fact correct. Thank you for pointing that out.



More information about the Python-list mailing list