Checking if string starts with list element
Alex Martelli
alex at magenta.com
Mon Aug 14 16:22:39 EDT 2000
"Simon Brunning" <SBrunning at trisystems.co.uk> wrote in message
news:31575A892FF6D1118F5800600846864D4C71D5 at intrepid...
> > Subject: Re: Checking if string starts with list element
> >
> >
> > > I want to know whether my sting *starts with* one of the strings in my
> > > list.
> >
> > The strings all seem to be words; can you get the first word in the
> > target string, and see if that is in the list?
> >
> > #Untested
> > import re
> > first_word = re.match('.*\b', us, word)
> > if first_word in romans:
> > # do stuff.
> > pass
> >
> Uh, interesting. Yes, I'm looking to match first words. I think that re is
> the way to go.
Nope, if full words is what you're after, then *definitely* a dictionary.
> One thing that I didn't mention is that the list is built up front, then
not
> changed. The matching process will happen several times. I can probably
> build and compile a nasty re statement up-front, and use it every time I
> need to do the match.
Yeah, but it ain't gonna cut the mustard compared with a dictionary.
Once you're done building the list of words (or, better, *instead* of
building the list of words):
audict={}
for r in romans:
audict[r]=1
For the testing-if-it's-there part:
first_word = firstWordOf(us)
if audict.has_key(first_word):
# do stuff
pass
If 'us' has whitespace-separated words, then
first_word, junk = string.split(us,1)
is fast. With the already-suggested re, you
can do finer processing (exclude punctuation
marks from words), if needed, at slightly
higher cost, though -- but I think it should
be:
at the start:
first_word_re = re.compile('\w+\b')
during the testing part:
first_word_match = first_word_re.match(us)
if first_word_match:
if audict.has_key(first_word_match.group()):
# do stuff
pass
But you're using re only to "extract first word"
if you need to. You sure you want to, btw? It's
a different spec than you gave earlier: by the
earlier spec, the list ['wine','bread'] should
have matched the string 'winery rampant!' while
by the present criteria it shouldn't.
Alex
More information about the Python-list
mailing list