Python 不可散列的类型:'dict' 类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27435798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unhashable type: 'dict' Type Error
提问by Morteza R
Suppose I have this dictionary:
假设我有这本词典:
items = {1: {'title': u'testing123', 'description': u'testing456'},
2: {'description': u'testing123', 'description': u'testing456'},
3: {'description': u'testing123', 'description': u'testing456'},
4: {'description': u'testing123', 'description': u'testing456'},
5: {'description': u'testing123', 'description': u'testing456'},
6: {'description': u'somethingelse', 'description': u'somethingelse'}}
I want to filter out the duplicate values, so that in the end I'd get
我想过滤掉重复的值,这样最后我会得到
{1: {'title': u'testing123', 'description': u'testing456'}, 6: {'title': u'something', 'description': u'somethingelse'}}
I wrote this code:
我写了这段代码:
dic = {}
for key, value in items.items():
if not set(value.values()).issubset(set(dic.values())):
dic[key] = value
however I get the error message TypeError: unhashable type: 'dict'. I am not sure why this happens and how to fix it.
但是我收到错误消息TypeError: unhashable type: 'dict'。我不确定为什么会发生这种情况以及如何解决它。
This is inspired by another questionand my failed attempt to solve it.
这是受到另一个问题的启发,我试图解决它失败。
采纳答案by James Sapam
dic.values() return list of dict
dic.values() 返回字典列表
>>> for key, value in items.items():
... print dic.values()
...
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
[{'description': u'testing456', 'title': u'testing123'}]
>>>
So, you can't apply set on dict as dict is not hashable.
因此,您不能在 dict 上应用 set,因为 dict 不可散列。
Btw you can fix it by:
顺便说一句,您可以通过以下方式修复它:
>>> dic = {}
>>> for key, value in items.items():
... if not set(value.values()).issubset(set(sum([x.values() for x in dic.values()],[]))):
... dic[key] = value
...
>>> dic
{1: {'description': u'testing456', 'title': u'testing123'}, 6: {'description': u'somethingelse', 'title': u'somethingelse'}}
>>>
For python > 3.x
对于 python > 3.x
if not set(value.values()).issubset(set(sum([list(x.values()) for x in list(dic.values())],[]))):
回答by Mike Graham
You are trying to create a set of dicts, but that's not possible, since dicts are unhashable (because they are mutable -- whether they are equal can change as you modify/add/remove pairs in the dict).
您正在尝试创建一组 dicts,但这是不可能的,因为 dicts 是不可散列的(因为它们是可变的——当您在 dict 中修改/添加/删除对时,它们是否相等可以改变)。
Perhaps instead of using the dicts, you can use tuples of their values for your set, a la if not set((v['description_a'], v['description_b]) for v in value.values()).issubset((v['description_a'], v['description_b]) for v in set(dic.values())):or similar?
也许不是使用 dicts,您可以使用它们的值的元组作为您的集合,a laif not set((v['description_a'], v['description_b]) for v in value.values()).issubset((v['description_a'], v['description_b]) for v in set(dic.values())):或类似的?
回答by xnx
Edit: If you must use a set, as others have noted you have to use a hashable object like a tuple:
编辑:如果你必须使用一个集合,正如其他人所指出的,你必须使用像元组这样的可散列对象:
unique_items = set()
for k, v in items.items():
sorted_v = tuple(sorted((k2, v2) for k2, v2 in v.items()))
unique_items.add(sorted_v)
unique_items = dict(unique_items)
gives for unique_items:
给unique_items:
{1: {'description': u'testing456', 'title': u'testing123'},
6: {'description': u'somethingelse', 'title': u'somethingelse'}}
If itemsis not huge (or, at least if the output dictionary isn't expected to be huge):
如果items不是很大(或者,至少如果预计输出字典不会很大):
items = {1: {'title': u'testing123', 'description': u'testing456'},
2: {'title': u'testing123', 'description': u'testing456'},
3: {'title': u'testing123', 'description': u'testing456'},
4: {'title': u'testing123', 'description': u'testing456'},
5: {'title': u'testing123', 'description': u'testing456'},
6: {'title': u'somethingelse', 'description': u'somethingelse'}}
unique_items = {}
for k, v in items.items():
if v not in unique_items.values():
unique_items[k] = v
(assuming the first key in your dictionary example should have been title). But you can't predict what the keys to this dictionary will be if duplicates do exist in items.
(假设您的字典示例中的第一个键应该是title)。但是,如果items.

