美文网首页
2019-06-13 python种对列表里的字典进行去重

2019-06-13 python种对列表里的字典进行去重

作者: 昨天今天下雨天1 | 来源:发表于2019-06-13 10:09 被阅读0次

python中列表去重的方法是set(),但是没有对列表里的字典进行去重的方法。只好自己写一个。

from functools import reduce


def list_dict_duplicate_removal(data_list):
    def run_function(x, y): return x if y in x else x + [y]
    return reduce(run_function, [[], ] + data_list)


if __name__ == '__main__':

    resource_list = [
        {'host': 'compute21', 'cpu': 2},
        {'host': 'compute21', 'cpu': 2},
        {'host': 'compute22', 'cpu': 2},
        {'host': 'compute23', 'cpu': 2},
        {'host': 'compute22', 'cpu': 2},
        {'host': 'compute23', 'cpu': 2},
        {'host': 'compute24', 'cpu': 2}
    ]

    resource_list = list_dict_duplicate_removal(resource_list)
    print(resource_list)

相关文章

网友评论

      本文标题:2019-06-13 python种对列表里的字典进行去重

      本文链接:https://www.haomeiwen.com/subject/wfhjfctx.html