我想删除停用词。这是我的代码
import nltk
from nltk.corpus import stopwords
import string
u="The apple is the pomaceous fruit of the apple tree, species Malus domestica in the rose family (Rosaceae). It is one of the most widely cultivated tree fruits, and the most widely known of the many members of genus Malus that are used by humans."
v="An orange is a fruit of the orangle tree. it is the most cultivated tree fruits"
u=u.lower()
v=v.lower()
u_list=nltk.word_tokenize(u)
v_list=nltk.word_tokenize(v)
for word in u_list:
if word in stopwords.words('english'):
u_list.remove(word)
for word in v_list:
if word in stopwords.words('english'):
v_list.remove(word)
print u_list
print "\n\n\n\n"
print v_list
但仅删除了一些停用词。请帮我解决这个问题
最佳答案
您所做的问题是 list.remove(x) 仅删除第一个出现的x
,而不是每个 x。要删除每个实例,您可以使用过滤器
,但我会选择这样的东西:
u_list = [word for word in u_list if word not in stopwords.words('english')]
关于python - 下面的python代码有什么错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702839/