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)