Efficient way of testing for substring being one of a set?

George Sakkis george.sakkis at gmail.com
Thu Apr 3 09:27:14 EDT 2008


On Apr 3, 9:00 am, Jeff <jeffo... at gmail.com> wrote:
> On Apr 3, 8:44 am, Ant <ant... at gmail.com> wrote:
>
>
>
> > On Apr 3, 12:37 pm, tinn... at isbd.co.uk wrote:
>
> > > What's the neatest and/or most efficient way of testing if one of a
>
> > A different approach:
>
> > >>> words = ["he", "sh", "bla"]
> > >>> name = "blah"
> > >>> True in (word in name for word in words)
>
> > True
>
> > >>> name = "bling"
> > >>> True in (word in name for word in words)
>
> > False
>
> > Perhaps not as obvious or readable as Jeff's example, but is
> > essentially doing the same thing using generator syntax.
>
> That's pretty :)

It's even prettier in 2.5:

any(word in name for word in words)

George



More information about the Python-list mailing list