类型错误:不可散列的类型:'dict' [英] TypeError: unhashable type: 'dict'

查看:42
本文介绍了类型错误:不可散列的类型:'dict'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码给了我一个错误unhashable type: dict 谁能向我解释一下解决方案是什么?

negids = movie_reviews.fileids('neg')def word_feats(words):返回 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):返回 dict([(word, True) for word in words if word not in stopset])结果=stopword_filtered_word_feats(negfeats)

解决方案

您正在尝试使用 dict 作为另一个 dict 或在 中的键>设置.这不起作用,因为键必须是可散列的.作为一般规则,只有不可变对象(字符串、整数、浮点数、冻结集、不可变元组)是可散列的(尽管可能有例外).所以这不起作用:

<预><代码>>>>dict_key = {"a": "b"}>>>some_dict[dict_key] = True回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:'dict'

要将 dict 用作​​键,您需要先将其转换为可以散列的内容.如果您希望用作键的字典仅包含不可变值,您可以像这样创建它的可散列表示:

<预><代码>>>>键 =frozenset(dict_key.items())

现在您可以使用 key 作为 dictset 中的键:

<预><代码>>>>some_dict[key] = True>>>some_dict{frozenset([('a', 'b')]): 真}

当然,当您想使用 dict 查找某些内容时,您需要重复练习:

<预><代码>>>>some_dict[dict_key] # 不起作用回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:'dict'>>>some_dict[frozenset(dict_key.items())] # 有效真的

如果您希望用作键的 dict 具有本身是 dict 和/或列表的值,您需要递归地冻结"预期键.这是一个起点:

def freeze(d):如果 isinstance(d, dict):返回frozenset((key, freeze(value)) for key, value in d.items())elif isinstance(d, list):return tuple(freeze(value) for value in d)返回

This piece of code is giving me an error unhashable type: dict can anyone explain to me what the solution is?

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)

解决方案

You're trying to use a dict as a key to another dict or in a set. That does not work because the keys have to be hashable. As a general rule, only immutable objects (strings, integers, floats, frozensets, tuples of immutables) are hashable (though exceptions are possible). So this does not work:

>>> dict_key = {"a": "b"}
>>> some_dict[dict_key] = True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

To use a dict as a key you need to turn it into something that may be hashed first. If the dict you wish to use as key consists of only immutable values, you can create a hashable representation of it like this:

>>> key = frozenset(dict_key.items())

Now you may use key as a key in a dict or set:

>>> some_dict[key] = True
>>> some_dict
{frozenset([('a', 'b')]): True}

Of course you need to repeat the exercise whenever you want to look up something using a dict:

>>> some_dict[dict_key]                     # Doesn't work
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> some_dict[frozenset(dict_key.items())]  # Works
True

If the dict you wish to use as key has values that are themselves dicts and/or lists, you need to recursively "freeze" the prospective key. Here's a starting point:

def freeze(d):
    if isinstance(d, dict):
        return frozenset((key, freeze(value)) for key, value in d.items())
    elif isinstance(d, list):
        return tuple(freeze(value) for value in d)
    return d

这篇关于类型错误:不可散列的类型:'dict'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆