原文引自:https://blog.csdn.net/weixin_37994148/article/details/99731818
第一種
def deleteDuplicate(li): func = lambda x, y: x if y in x else x + [y] li = reduce(func, [[], ] + li) return li
關於reduce(),請看http://docs.python.org/2/library/functions.html#reduce
第二種
def deleteDuplicate(li): temp_list = list(set([str(i) for i in li])) li=[eval(i) for i in temp_list] return li
第三種方法(python2中出錯)
[dict(t) for t in set([tuple(d.items()) for d in li])] # 解釋 li 是原始列表 d 是列表中的一個字典 t 是從字典中創建的元組之一 l = [{'a': 123, 'b': 1234}, {'a': 3222, 'b': 1234}, {'a': 123, 'b': 1234}] seen = set() new_l = [] for d in l: t = tuple(d.items()) if t not in seen: seen.add(t) new_l.append(d) print new_l