Python 类型错误:不可损坏的类型:';列表';用于文本摘要
我收到这个错误,我该怎么办Python 类型错误:不可损坏的类型:';列表';用于文本摘要,python,nlp,nltk,summarization,Python,Nlp,Nltk,Summarization,我收到这个错误,我该怎么办 from nltk.corpus import indian sentence_score={} #word=nltk.word_tokenize(text) for sent in sentences: word_count_in_sentence = (len(nltk.word_tokenize(sentence))) if word in wordfreq.keys(): if sent not in sentence_sco
from nltk.corpus import indian
sentence_score={}
#word=nltk.word_tokenize(text)
for sent in sentences:
word_count_in_sentence = (len(nltk.word_tokenize(sentence)))
if word in wordfreq.keys():
if sent not in sentence_score.keys():
sentence_score[sent]=wordfreq[word]
else:
senetence_score[sent]+=wordfreq[word]
使用
defaultdict
TypeError: unhashable type: 'list' for line 7 i.e if word in wordfreq.keys():
我们不知道什么是
word
,但它可能是一个列表,而不是您期望的列表…不要使用.keys()
,在hashmap(dict)中,搜索是O(1),但在键中搜索是线性的。似乎word
或wordfreq
是一个列表,请发布您的全部代码。如果不是在句子中发送的话:
要快得多
from nltk.corpus import indian
from collections import defaultdict
sentence_score=defaultdict(int)
#word=nltk.word_tokenize(text)
for sent in sentences:
word_count_in_sentence = (len(nltk.word_tokenize(sentence)))
if word in wordfreq:
senetence_score[sent]+=wordfreq[word]