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)