【发布时间】:2019-03-16 20:30:03
【问题描述】:
我有一个句子列表 (exg) 和一个水果名称列表 (fruit_list)。我有一个代码来检查句子是否包含来自fruit_list的元素,如下所示:
exg = ["I love apple.", "there are lots of health benefits of apple.",
"apple is especially hight in Vitamin C,", "alos provide Vitamin A as a powerful antioxidant!"]
fruit_list = ["pear", "banana", "mongo", "blueberry", "kiwi", "apple", "orange"]
for j in range(0, len(exg)):
sentence = exg[j]
if any(word in sentence for word in fruit_list):
print(sentence)
输出如下:只有句子包含带有“apple”的单词
I love apple.
there are lots of health benefits of apple.
apple is especially hight in Vitamin C,
但我想打印出哪个单词是fruit_list 的一个元素并且在句子中找到。在此示例中,我希望输出单词“apple”,而不是包含单词 apple 的句子。
希望这是有道理的。请发送帮助,非常感谢!
【问题讨论】:
-
嗯,你知道你想要什么 - 你尝试过什么实现它?
标签: python string python-3.x list if-statement