Collection set of data types
Set does not allow duplicate unordered collections and cannot be evaluated by subscript because unordered
1. Create
Create an empty collection
S ={} The default type is a dictionary, so it is not an empty collection, the empty collection is as follows
s = Set ()
s = {11,12,12,34,23} #字典是有键值对, the collection has no
s = set () #括号内可以接收可以迭代的元素, str list tuple dict
2. Conversion
s = ([11,123,1234,12345])
L = "123"
L = [1,2,3,4]
L = (1,2,3,4)
s = Set (L)
3. Methods
#集合中元素无序不重复
St = {11,22,33,44,55,66}
#集合中添加元素
St.add (+) print (ST)
A = {11,22,33,55,66}b = {55,66,77,11}
#diference方法
#a. Difference (b), a differs from the elements of B, and a remains the same
Reta = a.difference (b) print (a) print (Reta)
#b. Difference (a), B is different from element A, B remains unchanged
Print (b) Retb = B.difference (a) print (RETB)
#difference_update方法, find the element a differs from B and re-overwrite a
A.difference_update (b) print (a)
#找出b不同于a的元素并覆盖b
B.difference_update (a) print (b)
#找出a集合不同于b结合的的元素, the B collection is different from the elements of a set, and can be assigned to a new collection
Retab = a.symmetric_difference (b) retba = b.symmetric_difference (a) print (retab) print (RETBA)
#找出a集合不同于b结合的的元素, the B collection differs from the elements of a set and updates the found results to a combined
A.symmetric_difference_update (b) print (a)
#找出b集合不同于a结合的的元素, a sets the elements that are different from the B collection and updates the found results to the B combination
B.symmetric_difference_update (a) print (b)
#discard移除集合中的元素, if the element is removed, there is no error
Print (a) a.discard (one) print (a) A.discard (456) print (a)
#remove移除集合中的元素, if the element is removed, there is no error
Print (a) a.remove (a) print (a)
#111元素不存在与a中, the following will be an error
A.remove (111) A.remove ()
#intersaction取两个集合的交集, and can be assigned to a new collection
Retinab = a.intersection (b) print (RETINAB)
#a. Intersection_update (b) takes the intersection of two sets and adds the resulting collection to the collection A
A.intersection_update (b) print (a)
#并集union
#取a集合和b集合的并集
RETUAB = a.union (b) print (RETUAB)
#update将b集合更新追加到a集合中
A.update (b) print (a)
#a. Isdisjoint (b)
T = a.isdisjoint (b) print (t)
#a. Issubset (b) A is not a sub-sequence of b
Print (A.issubset (b))
#a. Issuperset (b) is not the parent sequence of B
Print (A.issuperset (b))
#a. Pop () to remove an element, you can assign the removed value to a variable
Ta = A.pop () print (TA)
#练习: Look for differences and update the old dictionary
Updates that are present in the #将old_dict, new_dict, old_dict deletions that do not exist in New_dict
#new_dict在old_dict中不存在的, append to old.
Old_dict = { "#1": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': +}, ' #2 ': {' hostname ': ' C1 ', ' Cpu_count ': 2 , ' mem_capicity ': " #3": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ':}}new_dict = { "#1": {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': + ', ' #3 ': {' hostname ': ' C1 ', ' Cpu_count ': 2, ' mem_capicity ': +}, ' #4 ' : {' hostname ': ' C2 ', ' Cpu_count ': 2, ' mem_capicity ':}}k1 = Old_dict.keys () K2 = New_dict.keys () K1_set = set (k1) K2_set = Set (k2) K1_d_k2 = k1_set.difference (k2_set) print (K1_D_K2) for I1 in K1_d_k2: old_dict.pop (i1) print (old_dict) k2_d_ K1 = k2_set.difference (k1_set) print (K2_D_K1) for i2 in K2_D_K1: old_dict[i2] = New_dict[i2]print (old_dict) k1_inter _K2 = k1_set.intersection (k2_set) print (K1_INTER_K2) for i3 in K1_INTER_K2: old_dict[i3] = New_dict[i3]print (old_ dict) print (old_dict==new_dict)
7. Set set of Python data type