【发布时间】:2019-07-09 02:57:13
【问题描述】:
我有一个嵌套字典,我正在尝试在其中查找重复项。例如,如果我有:
dictionary = {'hello': 3 , 'world':{'this': 5 , 'is':{'a': 3, 'dict': None}}}
返回值类似于:
True
因为这本词典包含重复项。
我可以很容易地用普通字典做到这一点,我认为这也适用于这种情况:
dictionary = {'hello': 3 , 'world':{'this': 5 , 'is':{'a': 3, 'dict': None}}}
rev_dictionary = {}
for key, value in dictionary.items():
rev_dictionary.setdefault(value, set()).add(key)
print(rev_dictionary)
for key,values in dictionary.items():
if len(values) > 1:
values = True
else:
values = False
这会引发以下错误:
TypeError: unhashable type: 'dict'
我怎样才能让它工作?
感谢您的帮助!
注意:如果可能,我更喜欢不使用库的解决方案
【问题讨论】:
-
Define "duplicates." 在我看来,即使给我
{"1": {"CPU": "AMD", "OS": "Linux", "Hostname": "spicy-tequila"}, "2": {"CPU": "AMD", "OS": "Linux", "Hostname": "coy-wolves"}},它们仍然是两个不同的对象,尽管它们有一些共同点。
标签: python dictionary