prefix matching in python

python

Solution

Try the following:

def prefix_match(sentence, taglist):
    taglist = tuple(taglist)
    for word in sentence.split():
        if word.startswith(taglist):
            return word

This works because `str.startswith()` can accept a tuple of prefixes as an argument.

Note that I renamed `string` to `sentence` so there isn't any ambiguity with the string module.

Problem

I have a string like: ``` " This is such an nice artwork" ``` and I have a tag_list `["art","paint"]` Basically, I want to write a function which accepts this string and taglist as inputs and returns me the word "artwork" as artwork contains the word art which is in taglist. How do i do this most efficiently? I want this to be efficient in terms of speed ``` def prefix_match(string, taglist): # do something here return word_in string ```

Original source