Reputation: 1245
I am trying to remove stop words (from nltk) from my data set but not sure why the one line query is not working:
filtered_words = [word if word not in stop_words for word in words]
This is what I need to do:
filtered_words = []
for word in words:
if word not in stop_words:
filtered_words.append(word)
Upvotes: 4
Views: 19122
Reputation: 1
The syntax is backwards. [appending_word for word in starting_words if word not in stop_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]'''
Upvotes: 0
Reputation: 943
syntax you want is :
x = [x for x in range(200) if x%3 == 0 ]
put condition behind for
the syntax you have requires else like :
x = [x if x%3 == 0 else None for x in range(200) ]
and this produces an error:
x = [x if x%3 == 0 for x in range(200) ]
Upvotes: 3
Reputation: 176
the if has to be at the end of the list comprehension:
filtered_words = [word for word in words if word not in stop_words]
see: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python
Upvotes: 9