我想从bananas-10
中提取thought1
,从10-bananas
提取thought2
。我该怎么做?我很难过,因为我不知道如何找出食物中的哪个食物被发现。
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 = ???
答案 0 :(得分:1)
我会使用这个列表comoprenhension方法:
text = thought1 + " " + thought2 # To handle both strings
result = [a for a in text.split() for b in foods if b in a]
print result
<强>输出:强>
['bananas-10', '10-bananas']