【发布时间】:2018-06-07 12:06:45
【问题描述】:
我知道关于这个确切的问题,这里有几个非常相似的答案,但没有一个能真正回答我的问题。
我正在尝试从单词列表中删除一系列停用词和标点符号以执行基本的自然语言处理。
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from string import punctuation
text = "Hello there. I am currently typing Python. "
custom_stopwords = set(stopwords.words('english')+list(punctuation))
# tokenizes the text into a sentence
sentences = sent_tokenize(text)
# tokenizes each sentence into a list of words
words = [word_tokenize(sentence) for sentence in sentences]
filtered_words = [word for word in words if word not in custom_stopwords]
print(filtered_words)
这会在filtered_words 行上引发TypeError: unhashable type: 'list' 错误。为什么会抛出这个错误?我根本不提供list 集合- 我提供的是set?
注意:我已经阅读了SO on this exact error 上的帖子,但仍然有同样的问题。接受的答案提供了这样的解释:
集合要求它们的项目是可散列的。超出预定义的类型 Python 只有不可变的,例如字符串、数字和元组, 是可散列的。 可变类型(例如列表和字典)不可散列 因为改变它们的内容会改变哈希并破坏 查找代码。
我这里提供了一组字符串,那为什么 Python 还在抱怨呢?
编辑:在阅读了此SO post(建议使用tuples)的更多内容后,我编辑了我的集合对象:
custom_stopwords = tuple(stopwords.words('english'))
我还意识到我必须展平我的列表,因为word_tokenize(sentence) 将创建一个列表列表,并且不会正确过滤掉标点符号(因为列表对象不会在custom_stopwords 中,这是一个字符串列表.
然而,这仍然引出了一个问题——为什么元组被 Python 认为是可散列的,而字符串集却不是?为什么TypeError 说list?
【问题讨论】:
-
试试this 发帖