- 列表和字典的去重
- lambda在字典的去重中的使用
# -*- coding: utf-8 -*-
def dedupe(items, key=None):
    seen = set()
    for item in items:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)
a = [1, 5, 2, 1, 9, 1, 5, 10]
result = list(dedupe(a, None))
print result
print set(a)
a = [{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
result = list(dedupe(a, key=lambda d: (d['x'], d['y'])))
print result
a = [{'x': 1, 'y': 2}, {'x': 1, 'y': 3}, {'x': 1, 'y': 2}, {'x': 2, 'y': 4}]
result = list(dedupe(a, key=lambda d: d['x']))
print result
[1, 5, 2, 9, 10]
[{'y': 2, 'x': 1}, {'y': 3, 'x': 1}, {'y': 4, 'x': 2}]
[{'y': 2, 'x': 1}, {'y': 4, 'x': 2}]