set 添加一个无序的,用set方法,访问速度快,天生解决了重复问题
1、difference 指定某个元素从原来set取出,并生成新的set
#difference
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.difference(["aaa"])
print(b) {'ccc', 'bbb', 'aaa'}
{'ccc', 'bbb'}
2、difference_update 删除原来set里某个元素
1 #difference_update
2 a = set(["aaa","bbb","ccc","aaa"])
3 print(a)
4 b = a.difference_update(["aaa"])
5 print(a)
6 print(b)
7
8
9 {'bbb', 'ccc', 'aaa'}
10 {'bbb', 'ccc'}
11 None
3、pop 删除某个元素
#pop
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.pop()
print(a)
print(b) {'aaa', 'bbb', 'ccc'}
{'bbb', 'ccc'}
aaa
4、remove 删除指定元素
#remove
a = set(["aaa","bbb","ccc","aaa"])
print(a)
b = a.remove("ccc")
print(a)
print(b) {'bbb', 'ccc', 'aaa'}
{'bbb', 'aaa'}
None
这里举一个例子,更新、删除、添加原来的表
old_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#2": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80}
}
# cmdb 新汇报的数据
new_dict = {
"#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 800},
"#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
"#4": {'hostname': 'c2', 'cpu_count': 2, 'mem_capicity': 80}
} a = set(old_dict.keys())
b = set(new_dict.keys()) #要更新的数据
update_set = a.intersection(b)
print(update_set)
#要删除的数据
delete_set = a.symmetric_difference(update_set)
print(delete_set)
#要添加的数据
add_set = b.symmetric_difference(update_set)
print(add_set) {'#3', '#1'}
{'#2'}
{'#4'}
set简单说明:
1 class set(object):
2 """
3 set() -> new empty set object
4 set(iterable) -> new set object
5
6 Build an unordered collection of unique elements.
7 """
8 def add(self, *args, **kwargs): # real signature unknown
9 """
10 Add an element to a set,添加元素
11
12 This has no effect if the element is already present.
13 """
14 pass
15
16 def clear(self, *args, **kwargs): # real signature unknown
17 """ Remove all elements from this set. 清除内容"""
18 pass
19
20 def copy(self, *args, **kwargs): # real signature unknown
21 """ Return a shallow copy of a set. 浅拷贝 """
22 pass
23
24 def difference(self, *args, **kwargs): # real signature unknown
25 """
26 Return the difference of two or more sets as a new set. A中存在,B中不存在
27
28 (i.e. all elements that are in this set but not the others.)
29 """
30 pass
31
32 def difference_update(self, *args, **kwargs): # real signature unknown
33 """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素"""
34 pass
35
36 def discard(self, *args, **kwargs): # real signature unknown
37 """
38 Remove an element from a set if it is a member.
39
40 If the element is not a member, do nothing. 移除指定元素,不存在不保错
41 """
42 pass
43
44 def intersection(self, *args, **kwargs): # real signature unknown
45 """
46 Return the intersection of two sets as a new set. 交集
47
48 (i.e. all elements that are in both sets.)
49 """
50 pass
51
52 def intersection_update(self, *args, **kwargs): # real signature unknown
53 """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """
54 pass
55
56 def isdisjoint(self, *args, **kwargs): # real signature unknown
57 """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False"""
58 pass
59
60 def issubset(self, *args, **kwargs): # real signature unknown
61 """ Report whether another set contains this set. 是否是子序列"""
62 pass
63
64 def issuperset(self, *args, **kwargs): # real signature unknown
65 """ Report whether this set contains another set. 是否是父序列"""
66 pass
67
68 def pop(self, *args, **kwargs): # real signature unknown
69 """
70 Remove and return an arbitrary set element.
71 Raises KeyError if the set is empty. 移除元素
72 """
73 pass
74
75 def remove(self, *args, **kwargs): # real signature unknown
76 """
77 Remove an element from a set; it must be a member.
78
79 If the element is not a member, raise a KeyError. 移除指定元素,不存在保错
80 """
81 pass
82
83 def symmetric_difference(self, *args, **kwargs): # real signature unknown
84 """
85 Return the symmetric difference of two sets as a new set. 对称差集
86
87 (i.e. all elements that are in exactly one of the sets.)
88 """
89 pass
90
91 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
92 """ Update a set with the symmetric difference of itself and another. 对称差集,并更新到a中 """
93 pass
94
95 def union(self, *args, **kwargs): # real signature unknown
96 """
97 Return the union of sets as a new set. 并集
98
99 (i.e. all elements that are in either set.)
100 """
101 pass
102
103 def update(self, *args, **kwargs): # real signature unknown
104 """ Update a set with the union of itself and others. 更新 """
105 pass