Reputation: 9840
I want to extract bananas-10
from thought1
and 10-bananas
from thought2
. How would I do this? I am stumped because I'm not sure how to figure out which item in foods was found in the string.
foods = ["bagels", "oranges", "bananas"]
thought1 = "I want some bananas-10"
thought2 = "I want some 10-bananas"
if any(food in thought1 for food in foods):
# extractedfood = ???
Upvotes: 1
Views: 810
Reputation: 34146
I would use this list comoprenhension approach:
text = thought1 + " " + thought2 # To handle both strings
result = [a for a in text.split() for b in foods if b in a]
print result
Output:
['bananas-10', '10-bananas']
Upvotes: 2