python列表里的字典元素去重
去重
1
2
3
4
5
6
7
8
|
from functools import reduce #导入排序模块 def list_dict_duplicate_removal(): data_list = [{ "a" : "123" , "b" : "321" }, { "a" : "123" , "b" : "321" }, { "b" : "321" , "a" : "123" }] run_function = lambda x, y: x if y in x else x + [y] return reduce(run_function, [[], ] + data_list) if __name__ == '__main__' : print list_dict_duplicate_removal() |
输出结果:
[{'a': '123', 'b': '321'}]
python列表中元素去重的几种方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
class StringReverse( object ): '' ' 列表去重,并按照原来的顺序排序 '' ' # 1.利用set方法和sort方法,原序 def string_duplicate_1(self, s): new_s = list( set (s)) # set 无序 new_s.sort(key=s.index) return new_s # 2.用列表中的元素作为字典中的key生成一个新字典,然后获取字典的key,非原序 def string_duplicate_2(self, s): a = {} # fromkeys(s,v)该方法的功能是生成一个字典,字典的key是 s中的值,s为可迭代对象,可以为str,tuple,list,set,dict,v为每一个key的值,默认为None return a.fromkeys(s).keys() # 3.利用defaultdict, 非原序 def string_duplicate_3(self, s): # 按照之前的顺序 from collections import defaultdict a = defaultdict() for x in s: a[x] = 0 return a.keys() # 4.最简单的循环,添加入新的列表,如果新列表中没有相同元素,则加入。原序 def string_duplicate_4(self, s): new_s = [] for x in s: if x not in new_s: new_s.append(x) return new_s # 5.利用itertools的groupby方法。非原序 def string_duplicate_5(self, s): from itertools import groupby s.sort() new_groupby = groupby(s) new_s = [] for x,y in new_groupby: new_s.append(x) return new_s # 6.reduce方法。非原序 def string_duplicate_6(self, s): return reduce(lambda x,y:x if y in x else x + [y], [[],] + s) if __name__ == "__main__" : stringReverse = StringReverse() s = [1,3,2,34,4,6,6,7,1,4,8,98] print "string_duplicate_1" , stringReverse.string_duplicate_1(s) print "string_duplicate_2" , stringReverse.string_duplicate_2(s) print "string_duplicate_3" , stringReverse.string_duplicate_3(s) print "string_duplicate_4" , stringReverse.string_duplicate_4(s) print "string_duplicate_5" , stringReverse.string_duplicate_5(s) print "string_duplicate_6" , stringReverse.string_duplicate_6(s) |
输出结果为:
1
2
3
4
5
6
|
string_duplicate_1 [1, 3, 2, 34, 4, 6, 7, 8, 98] string_duplicate_2 [1, 2, 3, 4, 98, 6, 7, 8, 34] string_duplicate_3 [1, 2, 3, 4, 98, 6, 7, 8, 34] string_duplicate_4 [1, 3, 2, 34, 4, 6, 7, 8, 98] string_duplicate_5 [1, 2, 3, 4, 6, 7, 8, 34, 98] string_duplicate_6 [1, 2, 3, 4, 6, 7, 8, 34, 98] |