需求:现在有一列表,列表中的元素为字典,现在要去重。
a = [{'name':'lilei','age':'18'},{'name':'tom','age':'16'},{'name':'lilei','age':'18'}]
第一反应会用到set
,但是会报错:
b = list(set(a))
# TypeError: unhashable type: 'dict'
只好遍历筛选每个元素:
from functools import reduce
a = [{'name':'lilei','age':'18'},{'name':'tom','age':'16'},{'name':'lilei','age':'18'}]
run_function = lambda x, y: x if y in x else x + [y]
uniqueList = reduce(run_function, [[], ] + a)