每天CookBook之Python-010

  • 列表和字典的去重
  • 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}]
posted @ 2016-07-09 09:42  4Thing  阅读(103)  评论(0)    收藏  举报