The list element is a dict, the method to remove duplicate data

The element of the list is a dictionary. The method to remove duplicate data in the list is as follows. The following method is mainly to remove multiple conditions:

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)


Guess you like

Origin blog.csdn.net/xxy_yang/article/details/85161043