从词典列表中删除重复项

时间:2022-11-11 04:41:32

I have following list of dictionaries:

我有以下词典列表:

d = [
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
{ 'name': 'test', 'regions': [{'country': 'US'}, {'country': 'DE'}] },
{ 'name': 'test 1', 'regions': [{'country': 'UK'}], 'clients': ['1', '2', '5'] },
{ 'name': 'test', 'regions': [{'country': 'UK'}] },
]

What is the easiest way to remove entries from the list that are duplicates ?

从列表中删除重复条目的最简单方法是什么?

I saw solutions that work, but only if an item doesn't have nested dicts or lists

我看到了可行的解决方案,但前提是项目没有嵌套的dicts或列表

2 个解决方案

#1


15  

How about this:

这个怎么样:

new_d = []
for x in d:
    if x not in new_d:
        new_d.append(x)

#2


0  

For easiest as in "easy to implement", the one in this question seems pretty compact.

对于最容易实现的“易于实现”,这个问题中的那个看起来非常紧凑。

Not likely it's also very effective performance-wise though.

尽管如此,它在性能方面也不太可能非常有效。

#1


15  

How about this:

这个怎么样:

new_d = []
for x in d:
    if x not in new_d:
        new_d.append(x)

#2


0  

For easiest as in "easy to implement", the one in this question seems pretty compact.

对于最容易实现的“易于实现”,这个问题中的那个看起来非常紧凑。

Not likely it's also very effective performance-wise though.

尽管如此,它在性能方面也不太可能非常有效。