Checking if string starts with list element

Simon Brunning SBrunning at trisystems.co.uk
Mon Aug 14 11:14:44 EDT 2000


I have a list of strings:
romans = ['aqueduct', 'sanitation', 'roads', 'irrigation', 'medicine',
'education', 'wine', 'public baths', 'order']

And I have a single string:
us = 'wine, women and song.'

I want to know whether my sting *starts with* one of the strings in my list.
The best that I can do is something like:

for roman in romans:
    if string.find(us, roman) == 0:
        # do stuff
        break

The trouble is that in the code I'm writing, the list has a hundred or so
elements, and it's running too slowly. Any suggestions?

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk





-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list