python list dict 去重的两种方式

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)


def deleteRepeat(lst):
    '''
    列表去重
    :param lst:
    :return:
    '''
    if not lst:
        return lst
    try:
        l = list(dedupe(lst, key=lambda k: (k[key1], k[key2])))
    except:
        l = []
        l.append(lst[0])
        for dict in lst:
            k = 0
            for item in l:
                if dict[key1] != item[key1] or dict[key2] != item[key2]:
                    k = k + 1
                else:
                    break
                if k == len(l):
                    l.append(dict)
    return l

 

转载于:https://www.cnblogs.com/danny-chen/p/6206731.html

你可能感兴趣的:(python)