TypeError: unhashable type: 'dict' Removing duplicates from compex List structure
TypeError: unhashable type: 'dict' Removing duplicates from compex List structure
我正在尝试从以下结构中删除重复项:
data = [
{
'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']
},
{
'author': 'Sham Isa',
'authorId': 23,
'tags': ['badminton', 'game']
},
{
'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']
}
]
我已尝试使用以下可用方法 here。
从词典列表中删除重复项的代码:
seen = set()
new_list = []
for d in data:
t = tuple(d.items())
if d not in seen:
seen.add(d)
new_list.append(d)
Remove duplicate dict in list in Python 中的解决方案不完全适用,因为您有内部列表。
您还需要对它们进行元组化才能在集合中使用。
一种方法是:
data = [{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']},
{'author': 'Sham Isa',
'authorId': 23,
'tags': ['badminton', 'game']},
{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']}]
seen = set()
new_list = []
for d in data:
l = []
# use sorted items to avoid {"a":1, "b":2} != {"b":2, "a":1} being
# different when getting the dicts items
for (a,b) in sorted(d.items()):
if isinstance(b,list):
l.append((a,tuple(b))) # convert lists to tuples
else:
l.append((a,b))
# convert list to tuples so you can put it into a set
t = tuple(l)
if t not in seen:
seen.add(t) # add the modified value
new_list.append(d) # add the original value
print(new_list)
输出:
[{'author': 'Isav Tuco', 'authorId': 62, 'tags': ['fire', 'works']},
{'author': 'Sham Isa', 'authorId': 23, 'tags': ['badminton', 'game']}]
虽然这是被黑了——你可能想要自己更好的解决方案。
我正在尝试从以下结构中删除重复项:
data = [
{
'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']
},
{
'author': 'Sham Isa',
'authorId': 23,
'tags': ['badminton', 'game']
},
{
'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']
}
]
我已尝试使用以下可用方法 here。 从词典列表中删除重复项的代码:
seen = set()
new_list = []
for d in data:
t = tuple(d.items())
if d not in seen:
seen.add(d)
new_list.append(d)
Remove duplicate dict in list in Python 中的解决方案不完全适用,因为您有内部列表。
您还需要对它们进行元组化才能在集合中使用。
一种方法是:
data = [{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']},
{'author': 'Sham Isa',
'authorId': 23,
'tags': ['badminton', 'game']},
{'author': 'Isav Tuco',
'authorId': 62,
'tags': ['fire', 'works']}]
seen = set()
new_list = []
for d in data:
l = []
# use sorted items to avoid {"a":1, "b":2} != {"b":2, "a":1} being
# different when getting the dicts items
for (a,b) in sorted(d.items()):
if isinstance(b,list):
l.append((a,tuple(b))) # convert lists to tuples
else:
l.append((a,b))
# convert list to tuples so you can put it into a set
t = tuple(l)
if t not in seen:
seen.add(t) # add the modified value
new_list.append(d) # add the original value
print(new_list)
输出:
[{'author': 'Isav Tuco', 'authorId': 62, 'tags': ['fire', 'works']},
{'author': 'Sham Isa', 'authorId': 23, 'tags': ['badminton', 'game']}]
虽然这是被黑了——你可能想要自己更好的解决方案。