Python删除带有重复字段的dict项

时间:2012-02-13 06:41:14

标签: python

我有以下的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'}]

1 个答案:

答案 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列表没有重复项。