一、字符串/数值型的简单列表,用set即可
#!/usr/bin/python
# -*- coding=utf-8 -*-
list1 = [1, 3, '3', 1, 'aa', 'aa']
print(list(set(list1)))
输出:['aa', 1, '3', 3]
二、字典list,用set会报错,需要封装方法
错误示例:
list1 = [{"张三": 20, "李四": 21}, {"李四": 21}, {"李四": 21, "张三": 20}]
print(list(set(list1)))
输出:TypeError: unhashable type: 'dict'
正确方案:
#!/usr/bin/python
# -*- coding=utf-8 -*-
from functools import reduce # 导入排序模块
def list_dict_duplicate_removal(dict_list):
'''
列表去重
Returns:
'''
run_function = lambda x, y: x if y in x else x + [y]
return reduce(run_function, [[], ] + dict_list)
list1 = [{"张三": 20, "李四": 21}, {"李四": 21}, {"李四": 21, "张三": 20}]
print(list_dict_duplicate_removal(list1))
输出:[{'张三': 20, '李四': 21}, {'李四': 21}]