简体   繁体   English

从词典列表中删除重复项

[英]Remove duplicates from the list of dictionaries

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或列表

How about this: 这个怎么样:

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

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. 尽管如此,它在性能方面也不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM