You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Python代码触发TypeError: unhashable type: 'list'求助

问题根因

你的报错出在词形还原的列表推导式代码行,你错误地把**已经分割为列表的整行文本变量x**传给了lemmatizer.lemmatize()方法,而该方法要求传入的是单个字符串类型的词汇,同时你后续尝试用这个列表做判断/处理时,才触发了不可哈希类型:list的报错。

具体错误行是你写的:

x = [lemmatizer.lemmatize(x) for word in x if word not in stop_words]

这里遍历x(分割后的词列表)的每一个元素word,但你调用lemmatize方法时传的是整个列表x,而非遍历得到的单个词word。

修正方案

把该行的入参改成遍历得到的word即可,修正后的完整词形还原块代码如下:

#lemmatization
from nltk.corpus import stopwords
nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
tokens=[]
from nltk.stem import WordNetLemmatizer
nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()
for x in y:
    x = str(x)
    x = x.lower()
    x = x.split()
    # 修正:把入参从x改为word
    x = [lemmatizer.lemmatize(word) for word in x if word not in stop_words]
    x = ' '.join(x)
    tokens.append(x)
print(tokens)

内容的提问来源于stack exchange,提问作者Vaishnavi Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 00:15:07