簡體   English   中英

從詞典列表中刪除重復項

[英]Remove duplicates from the 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'}] },
]

從列表中刪除重復條目的最簡單方法是什么?

我看到了有效的解決方案,但前提是項目沒有嵌套的dicts或列表

這個怎么樣:

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

對於最容易實現的“易於實現”, 這個問題中的那個看起來非常緊湊。

盡管如此,它在性能方面也不太可能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM