Python3:比较列表中的多个字典/删除重复条目

4

我有一个字典列表,看起来是这样的:

[{'direction': 'PLACE_A', 
    'line': 'S2', 
    'delay': 0, 
    'when': '2017-11-29T17:48:00+01:00', 
    'trip': 33738
    }

{'direction': 'PLACE_A', 
    'line': 'S2', 
    'delay': 0, 
    'when': '2017-11-29T17:48:00+01:00', 
    'trip': 33738
}
{'direction': 'PLACE_B', 
    'line': 'S1', 
    'delay': 0, 
    'when': '2017-11-29T17:51:00+01:00', 
    'trip': 33739
}
{'direction': 'PLACE_B', 
    'line': 'S1', 
    'delay': 0, 
    'when': '2017-11-29T17:55:00+01:00', 
    'trip': 33740
}
{'direction': 'PLACE_A', 
    'line': 'S2', 
    'delay': 0, 
    'when': '2017-11-29T17:48:00+01:00', 
    'trip': 33738
}]

一些条目出现了两次或更多:其中trip的值是相同的。

如何以删除字典列表中的重复条目的方式缩小列表?我尝试过使用 set,但在这里没有用。

非常感谢任何帮助。谢谢!


3
这符合您的需求吗?https://dev59.com/9Wgu5IYBdhLWcg3wxZpL - Alan
@alan 我认为问题不同,那里只检查一个 id 值是否唯一,而这里的操作是想要检查两个字典是否相同。 - Mohsen_Fatemi
@Moshen。我也是。ID 是 trip - Mad Physicist
https://dev59.com/lWox5IYBdhLWcg3wHAsA - chakradhar kasturi
@Alan 非常感谢。这对我来说很有效。 - hildwin
1个回答

0

只需创建一个新列表:

newlist = []
for item in oldlist:
    if item not in newlist:
        newlist.append(item)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接