从词典列表中删除重复项

2024-04-27 05:29:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下列词典:

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'}] },
]

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

我看到了有效的解决方案,但前提是项目没有嵌套的dict或list


Tags: 项目方法nametest列表de解决方案country
2条回答

这个怎么样:

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

对于“易于实现”中最简单的一个来说,this question中的那个看起来非常紧凑。

不过,这也不太可能是非常有效的性能。

相关问题 更多 >