我对Python还不熟悉。在这里,我测试了在循环中使用varialbewordlist
的示例。你知道吗
我需要在使用它之前手动声明它吗?或者它会被声明为运行时?你知道吗
我甚至尝试了手动声明,但它仍然是空的。你知道吗
我遵循这个例子:http://www.sjwhitworth.com/sentiment-analysis-in-python-using-nltk/
如果我直接用这个:
wordlist = [i for i in wordlist if not i in stopwords.words('english')]
wordlist = [i for i in wordlist if not i in customstopwords]
它给出了错误:
wordlist = [i for i in wordlist if not i in stopwords.words('english')]
NameError: name 'wordlist' is not defined
我手动声明wordlist
如下
wordlist = []
但在这种情况下它仍然是空的:
wordlist = []
wordlist = [i for i in wordlist if not i in stopwords.words('english')]
wordlist = [i for i in wordlist if not i in customstopwords]
print wordlist
我在这里做错什么了?你知道吗
下面是列表理解在python中的工作方式。假设你有一个列表
x
,比如您希望使用列表理解增加其值:
这将输出:
在您的例子中
x
(即wordlist
)是空的,因此for
循环不会迭代。根据前面提到的link的描述wordlist
应该是艺术家名字的数组。你知道吗你的第一份理解清单:
大致相当于:
当你这样读的时候,很明显
wordlist
必须是一个可以理解的东西,然后才能理解它。当然,什么是wordslist
应该由你决定,完全取决于你想要完成的事情。。。你知道吗相关问题 更多 >
编程相关推荐