prefix matching

Stefan Meier smeier at exelixis.com
Wed May 26 15:33:02 EDT 2004


Must be something like

>>> l=["pref1","pref2"]
>>> s="String"
>>> if filter(lambda x:s[:len(x)] == x, l):
>>>     # do something

Cheers,
Stefan

On Wednesday 26 May 2004 12:08 pm, Christian Gudrian wrote:
> Hello, there!
>
> Given a list with strings.
>
> 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?
>
> Christian

-- 
Stefan Meier
Computational Biology & Informatics
Exelixis, Inc.
170 Harbor Way, P.O. Box 511
South San Francisco, CA 94083-511
fon. (650)837 7816




More information about the Python-list mailing list