python list dict 去重的两种方式

def dedupe(items, key=None):
seen = set()
for item in items:
val = item if key is None else key(item)
if val not in seen:
yield item
seen.add(val) def deleteRepeat(lst):
'''
列表去重
:param lst:
:return:
'''
if not lst:
return lst
try:
l = list(dedupe(lst, key=lambda k: (k[key1], k[key2])))
except:
l = []
l.append(lst[0])
for dict in lst:
k = 0
for item in l:
if dict[key1] != item[key1] or dict[key2] != item[key2]:
k = k + 1
else:
break
if k == len(l):
l.append(dict)
return l
上一篇:python连接redis数据库的两种方式


下一篇:python数字转换为字符串的两种方式