On Apr 27, 11:43 am, jazbees <jazb...@gmail.com> wrote: > On Apr 25, 12:11 pm, Duncan Booth <duncan.bo...@invalid.invalid> > wrote: > > > jazbees <jazb...@gmail.com> wrote: > > >>>> hasvowels = lambda x:max([y in x for y in "aeiou"]) > > >>>> hasvowels("parsnips") > > > True > > >>>> hasvowels("sfwdkj") > > > False > > > Do you object to using def to define functions? > > Not at all. Do you object to my use of lambdas? I'm not aware of > anything that says it's bad form to define a function using a lambda > when the only thing that a function does is immediately return some > calculated value. > > > Anyway, it is probably clearer to use the builtin 'any' for code like this: > > > >>> def hasvowels(s): > > > ... return any(v in s for v in "aeiou") > > I wasn't aware of either "any" or "all". Thanks for the info! > Unfortunately this recent project where I used "min" and "max" is > running on a system using Python 2.4, so "any" and "all" are not > available. > > > If you are doing a lot of this consider whether you might be better off > > using sets: > > > >>> def found(x,y): > > > ... return bool(set(x).intersection(y)) > > I haven't used sets very much, but I'll definitely keep this in mind.
Something else worth noting: dos-prompt>\python24\python -mtimeit -s"hasvowels=lambda x:max([y in x for y in 'aeiou'])" "hasvowels('parsnips')" 100000 loops, best of 3: 3.08 usec per loop dos-prompt>\python24\python -mtimeit -s"hasvowels=lambda x:max([y in x for y in 'aeiou'])" "hasvowels('qwrtypsdf')" 100000 loops, best of 3: 3.06 usec per loop dos-prompt>\python24\python -mtimeit -s"import re; hasvowels=re.compile ('[aeiou]').search" "hasvowels('parsnips')" 1000000 loops, best of 3: 1.32 usec per loop dos-prompt>\python24\python -mtimeit -s"import re; hasvowels=re.compile ('[aeiou]').search" "hasvowels('qwrtypsdf')" 1000000 loops, best of 3: 0.934 usec per loop HTH, John -- http://mail.python.org/mailman/listinfo/python-list