我是Python编码的新手。我想使用if-else循环将Sample_text中的句子分为2个不同的列表,基于该单词是水果还是非水果单词。
Sample_text = """Blend mangos puree, almond milk, protein powder, peanut butter, and strawberries in a blender until smooth. Serve immediately."""
fruits = ["bannanas","acai","blueberries","strawberries","mangos"]
fruit_words = []
non_fruit_words = []
for word in Sample_text.split(" "):
if
else
答案 0 :(得分:0)
我希望这就是你要的。
{"name":"Foo"}
输出:
Sample_text = """Blend mangos puree, almond milk, protein powder, peanut butter, and strawberries in a blender until smooth. Serve immediately."""
fruits = ["bannanas","acai","blueberries","strawberries","mangos"]
fruit_words = []
non_fruit_words = []
for word in Sample_text.split(" "):
if word in fruits:
fruit_words.append(word)
else:
non_fruit_words.append(word)
print(f"{fruit_words}\n{non_fruit_words}")