1
word_list = "love does not make the world go round. love is what makes the ride worthwhile" 
from nltk.corpus import stopwords
for word in word_list:
    #print word
    if word in stopwords.words('english'):
       #print word #print out stopword for checking
       word_list.remove(word)
    else:
       print word

例如......在我的word_list中......我有“爱不会让世界运转。爱是让骑行值得的东西”

我想打印出所有不在停用词中的单词...

但它只打印出love,make,go,round,love,makes,worthy.......“世界,骑行”这个词没有打印出来......有人知道如何解决吗?谢谢...

4

1 回答 1

1

如果您更改word_list, 使其成为单词列表,则可以正常工作。word_list将包含您所追求的单词。

word_list = ['love','does','not','make','the','world','go','round','love',
             'is','what','makes','the','ride','worthwhile']
#your code: 
from nltk.corpus import stopwords
for word in word_list:
    #print word
    if word in stopwords.words('english'):
       #print word #print out stopword for checking
       word_list.remove(word)
    else:
       print word   
#now put:
print word_list
#output:
['love', 'not', 'make', 'world', 'go', 'round', 'love', 'what',
 'makes', 'ride', 'worthwhile']
于 2012-04-16T19:11:31.117 回答