python 字典列表/列表套字典 去重重复的字典数据
原文引自: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
相关推荐
坚持是一种品质 2020-11-16
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16