检查字符串是否包含单词列表中单词的子集

时间:2014-01-19 16:52:11

标签: python

我想从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 = ???

1 个答案:

答案 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']
相关问题