prefix matching

Peter Otten __peter__ at web.de
Wed May 26 16:02:48 EDT 2004


Christian Gudrian wrote:

> What is the most pythonic way to check if a given string starts with one
> of the strings in that list?
> 
> I started by composing a regular expression pattern which consists of all
> the strings in the list separated by "|" in a for loop. Then I used that
> pattern to do a regexp match.
> 
> Seems rather complicated to me. Any alternatives?

Taken from the itertools examples at
http://docs.python.org/lib/itertools-example.html

>>> import itertools
>>> True in itertools.imap("so what".startswith, ["so", "what", "else"])
True
>>> True in itertools.imap("so what".startswith, ["what", "else"])
False
>>>

It's wrapped in a function - any() - there.

Peter




More information about the Python-list mailing list