如何使用拆分功能和If-Else根据关键字将句子拆分为2个列表

时间:2020-07-04 22:26:22

标签: python list loops if-statement split

我是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

1 个答案:

答案 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}")