list元素为dict,去除重复数据的方法

list的元素是字典,去除list重复数据的实现方法如下下面方法主要是多个条件去重:

def distinct_list(datas):
    data_list = []
    data_list.append(datas[0])
    for dict in datas:
        k = 0
        for item in data_list:
            if dict['name'] == item['name'] and dict['number'] == item['number'] :
                break
            else:
                k = k + 1
            if k == len(data_list):
                data_list.append(dict)
    return  data_list
第二种方法,是利用python的reduce:
    from functools import reduce
    run_function = lambda x,y : x if y in x else x + [y]
    data = reduce(run_function,[[],] + list_data)


你可能感兴趣的:(list元素为dict,去除重复数据的方法)