从字典数组中提取重复项

时间:2014-04-27 17:09:52

标签: python arrays dictionary

嗨,我有一系列的dicts,如下所示:

books = [
         {'Serial Number': '3333', 'size':'500', 'Book':'The Hobbit'},
         {'Serial Number': '2222', 'size':'100', 'Book':'Lord of the Rings'},
         {'Serial Number': '1111', 'size':'200', 'Book':'39 Steps'},
         {'Serial Number': '3333', 'size':'600', 'Book':'100 Dalmations'},
         {'Serial Number': '2222', 'size':'800', 'Book':'Woman in Black'},
         {'Serial Number': '6666', 'size':'1000', 'Book':'The Hunt for Red October'},
        ]

我需要根据重复的序列号创建一个单独的dicts数组:

duplicates = [
    '3333', [{'Book':'The Hobbit'}, {'Book':'100 Dalmations'}],
    '2222', [{'Book':'Lord of the Rings'}, {'Book':'Woman in Black'}]
]

使用内置函数是否有一种简单的方法可以做到这一点,如果不是最好的方法来实现这一点?

1 个答案:

答案 0 :(得分:0)

我能想到的最蟒蛇的方式:

from collections import defaultdict
res = defaultdict(list)

for d in books:
    res[d.pop('Serial Number')].append(d)

print({k: v for k, v in res.items() if len(v) > 1})

输出:

{'2222': [{'Book': 'Lord of the Rings', 'size': '100'},
          {'Book': 'Woman in Black', 'size': '800'}],
 '3333': [{'Book': 'The Hobbit', 'size': '500'},
          {'Book': '100 Dalmations', 'size': '600'}]}
相关问题