I'm trying to remove the stopwords from a user input string using the .join
function. It looks like this:
while True:
line = raw_input()
if line.strip() == stopword:
break
remove_stopwords = ''.join(word for word in line.split() if word not in stop_words)
I've defined stop_words
in a list at the top. The problem is that when I type in the string for the stop words to be removed from, it only removes the first word and leaves the rest. Any help would be great. I'm new to this so it's probably something stupid.
Here is a one liner using the
filter
function:Additionally, consider storing your stop words in a
set
rather than alist
. The average algorithmic complexity of the search operation (in
) is constant for aset
and linear for alist
.Edit: Your program appears to be working as expected with an additional space for the
join
string. This makes sense as(x for x in y if f(x))
is roughly equivalent tofilter
:input:
output:
Your bug must be somewhere else in your program. What else are you doing?