On Feb 15, 11:31 pm, odeits <ode...@gmail.com> wrote: > On Feb 15, 9:56 pm, Chris Rebert <c...@rebertia.com> wrote: > > > > > On Sun, Feb 15, 2009 at 9:17 PM, <pyt...@bdurham.com> wrote: > > > I need to test strings to determine if one of a list of chars is in the > > > string. A simple example would be to test strings to determine if they > > > have > > > a vowel (aeiouAEIOU) present. > > > > I was hopeful that there was a built-in method that operated similar to > > > startswith where I could pass a tuple of chars to be tested, but I could > > > not > > > find such a method. > > > > Which of the following techniques is most Pythonic or are there better > > > ways > > > to perform this type of match? > > > > # long and hard coded but short circuits as soon as match found > > > if 'a' in word or 'e' in word or 'i' in word or 'u' in word or ... : > > > > -OR- > > > > # flexible, but no short circuit on first match > > > if [ char for char in word if char in 'aeiouAEIOU' ]: > > > Just use the fairly new builtin function any() to make it short-circuit: > > > if any(char.lower() in 'aeiou' for char in word): > > do_whatever() > > > Cheers, > > Chris > > > -- > > Follow the path of the Iguana...http://rebertia.com > > If you want to generalize it you should look at > setshttp://docs.python.org/library/sets.html > > 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 -- http://mail.python.org/mailman/listinfo/python-list