Saturday, 14 September 2013

Grab the first word in a list that is found in a string. ( Python )

Grab the first word in a list that is found in a string. ( Python )

So, I have a list of words like so:
activationWords = ['cactus', 'cacti', 'rofl']
And I want to find any of those words and return the first word of any of
those words appearing in a random string. I'll use this string as an
example:
str = "Wow, rofl I found a cactus in a cacti pile."
As you can see with the above example string, the first instance of a word
in the list is "rofl". I want to be able to detect that and return the
word into a string that I can use to my discretion. How would I do this?
Keep in mind that that string is just an example. Each time I run this it
will be using a different string.

No comments:

Post a Comment