我有以下的dicts列表。我想删除对象字段的重复项。如果该对象已经出现,删除该项目的最简单方法是什么?
[{'date': datetime.datetime(2012, 2, 12, 21, 42, 54, 634699),
'object': <Subscription: Subscription object>,
'subscription': <Subscription: Subscription object>,
'type': 'subscription'},
{'date': datetime.datetime(2012, 2, 12, 21, 25, 51, 51554),
'object': <Subscription: Subscription object>,
'subscription': <Subscription: Subscription object>,
'type': 'subscription'},
{'date': datetime.datetime(2012, 2, 12, 20, 59, 18, 211191),
'object': <Subscription: Subscription object>,
'subscription': <Subscription: Subscription object>,
'type': 'subscription'},
{'date': datetime.datetime(2012, 2, 12, 18, 1, 37, 43482),
'object': <Subscription: Subscription object>,
'subscription': <Subscription: Subscription object>,
'type': 'subscription'},
{'date': datetime.datetime(2012, 2, 12, 18, 1, 24, 322306),
'object': <TipList: mulligan - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'},
{'date': datetime.datetime(2012, 2, 9, 23, 33, 17, 511657),
'object': <TipList: mulligan - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'},
{'date': datetime.datetime(2012, 2, 7, 16, 18, 51, 597775),
'object': <TipList: mulligan - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'},
'object': <TipList: mulligan - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'},
{'date': datetime.datetime(2012, 2, 7, 10, 9, 12, 975944),
'object': <TipList: mulligan - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'},
{'date': datetime.datetime(2012, 1, 19, 20, 37, 26, 663267),
'object': <TipList: a - Buenos Aires>,
'subscription': <Subscription: Subscription object>,
'type': 'tiplist'}]
答案 0 :(得分:6)
假设__hash__
和Subscription
类的TipList
方法正确实现,我会做如下的事情:
seen = set()
new_l = []
for item in l:
obj = item['object']
if obj not in seen:
seen.add(obj)
new_l.append(item)
其中l
是原始列表,new_l
列表没有重复项。