一行用于在Python中将元素添加到列表的循环

时间:2019-07-02 21:49:16

标签: python for-loop

我正在尝试从数据集中删除停用词(从nltk),但是不确定为什么单行查询不起作用:

filtered_words = [word if word not in stop_words for word in words]

这是我需要做的:

filtered_words = []
for word in words:
    if word not in stop_words:
        filtered_words.append(word)

3 个答案:

答案 0 :(得分:4)

如果必须在列表理解的末尾:

filtered_words = [word for word in words if word not in stop_words]

请参阅:https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

答案 1 :(得分:2)

您想要的语法是:

x = [x for x in range(200) if x%3 == 0 ]

添加条件

您使用的语法还需要其他类似的内容:

x = [x if x%3 == 0  else None for x in range(200)  ]

这会产生错误:

x = [x if x%3 == 0  for x in range(200)  ]

答案 2 :(得分:0)

语法是向后的。 [如果单词不在stop_words中,则在start_words中添加单词的单词]


starting_words = ["hi", "joshing", "afflate", "damage"]
stop_words = ["afflate", "K", "books"]
filtered_words = []
'''for word in starting_words:
    if word not in stop_words:
        filtered_words.append(word)
==

filtered_words = [word for word in starting_words if word not in stop_words]'''