TypeError:不可用类型:'dict'

时间:2012-11-07 06:47:25

标签: python

这段代码给我一个错误unhashable type: dict任何人都可以解释我的解决方案

negids = movie_reviews.fileids('neg')
def word_feats(words):
    return dict([(word, True) for word in words])

negfeats = [(word_feats(movie_reviews.words(fileids=[f])), 'neg') for f in negids]
stopset = set(stopwords.words('english'))

def stopword_filtered_word_feats(words):
    return dict([(word, True) for word in words if word not in stopset])

result=stopword_filtered_word_feats(negfeats)

1 个答案:

答案 0 :(得分:2)

可能的解决方案可能是使用JSON dumps()方法,因此您可以将字典转换为字符串-

import json
a={"a":10, "b":20}
b={"b":20, "a":10}
c = [json.dumps(a), json.dump(b)]
set(c)
json.dump(a) in c

输出-

set(['{"a": 10, "b": 20}'])
True