list中的字典去重

list中的字典: 格式

list_dict = [{"a": "123", "b": "321"}, {"a": "1223", "b": "321"}, {"b": "321", "a": "123"}]

 

如上,list_dict中有三个字典,但是是重复的,这里需要去重,保留一个不重复的dict即可

def list_dict_duplicate_removal(list_dict):
    """list中dict重复的话,去重"""
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + list_dict)


all_test_data = list_dict_duplicate_removal(test_data)
print(f"去重,拿到所有商品信息,总数为:{len(list_dict)}")

输出如下内容:

 

带条件去重

def list_dict_duplicate_removal(list_dict):
    """list中dict重复的话,去重"""
    run_function = lambda x, y: x if dict(list(y.items())[:-1]) in [dict(list(a.items())[:-1]) for a in x] else x + [y]
    return reduce(run_function, [[], ] + list_dict)

 

posted @ 2020-07-31 10:21  小心走火  阅读(777)  评论(0编辑  收藏  举报