Python 列表里字典元素的去重

def list_dict_duplicate_removal():
    data_list = [{"a": "123", "b": "321"}, {"a": "123", "b": "321"}, {"b": "321", "a": "123"}]
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + data_list)


if __name__ == '__main__':
    print list_dict_duplicate_removal()
输出结果:

[{'a': '123', 'b': '321'}]

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。

posted @ 2018-11-20 20:22  张皓钧z  阅读(588)  评论(0编辑  收藏  举报