set 集合

set 是一个无序且不重复的元素集合

访问速度快

解决了重复问题

 1     def add(self, *args, **kwargs): # real signature unknown 添加
 2         """
 3         Add an element to a set.
 4         
 5         This has no effect if the element is already present.
 6         """
 7         pass
例:
s1 = set() s1.add('xiaolong') print(s1) s1.add('xiaolong') print(s1)

得:

   {'xiaolong'}
   {'xiaolong'}

 8 
 9     def clear(self, *args, **kwargs): # real signature unknown清空所有数据
10         """ Remove all elements from this set. """
11         pass

12 13 def copy(self, *args, **kwargs): # real signature unknown浅拷贝 14 """ Return a shallow copy of a set. """ 15 pass

16 17 def difference(self, *args, **kwargs): # real signature unknown比对两个set之间的不同、得到一个新set 18 """ 19 Return the difference of two or more sets as a new set. 20 21 (i.e. all elements that are in this set but not the others.) 22 """ 23 pass
例:
s2 = set(['alex', 'eric', 'tony','alex']) print(s2) s3 = s2.difference(['alex', 'eric']) print(s2) print(s3)
得:

{'tony', 'alex', 'eric'}
{'tony', 'alex', 'eric'}
{'tony'}


24
25 def difference_update(self, *args, **kwargs): # real signature unknown更新本身数据集合、把传进来的数据和本身一样的删掉,没有返回值(删除当前set中的所有包含在 参数集合 里的元素) 26 """ Remove all elements of another set from this set. """ 27 pass
例:
s2 = set(['alex', 'eric', 'tony','alex']) print(s2) s4 = s2.difference_update(['alex', 'eric'])#更新了自己、把以前的和传进来的相同的移除 print(s2) print(s4)
得:

{'alex', 'tony', 'eric'}
{'tony'}
None


28 
29     def discard(self, *args, **kwargs): # real signature unknown移除元素
30         """
31         Remove an element from a set if it is a member.
32         
33         If the element is not a member, do nothing.
34         """
35         pass

36 37 def intersection(self, *args, **kwargs): # real signature unknown取交集,创建一个新的set 38 """ 39 Return the intersection of two sets as a new set. 40 41 (i.e. all elements that are in both sets.) 42 """ 43 pass 44
45 def intersection_update(self, *args, **kwargs): # real signature unknown取交集、修改原来的set 46 """ Update a set with the intersection of itself and another. """ 47 pass

48 49 def isdisjoint(self, *args, **kwargs): # real signature unknown如果没有交集、返回true 50 """ Return True if two sets have a null intersection. """ 51 pass

52 53 def issubset(self, *args, **kwargs): # real signature unknown是否是子集 54 """ Report whether another set contains this set. """ 55 pass

56 57 def issuperset(self, *args, **kwargs): # real signature unknown是否是父集 58 """ Report whether this set contains another set. """ 59 pass

60 61 def pop(self, *args, **kwargs): # real signature unknown移除 62 """ 63 Remove and return an arbitrary set element. 64 Raises KeyError if the set is empty. 65 """ 66 pass 67 68 def remove(self, *args, **kwargs): # real signature unknown移除 69 """ 70 Remove an element from a set; it must be a member. 71 72 If the element is not a member, raise a KeyError. 73 """ 74 pass

例:
s2 = set(['alex', 'eric', 'tony','alex']) print(s2) s2.remove('alex') print(s2)
得:

{'eric', 'tony', 'alex'}
{'eric', 'tony'}




75 76 def symmetric_difference(self, *args, **kwargs): # real signature unknown差集、创建新对象 77 """ 78 Return the symmetric difference of two sets as a new set. 79 80 (i.e. all elements that are in exactly one of the sets.) 81 """ 82 pass

83 84 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown差集、改变原来 85 """ Update a set with the symmetric difference of itself and another. """ 86 pass 87
88 def union(self, *args, **kwargs): # real signature unknown并集 89 """ 90 Return the union of sets as a new set. 91 92 (i.e. all elements that are in either set.) 93 """ 94 pass 95 96 def update(self, *args, **kwargs): # real signature unknown更新 97 """ Update a set with the union of itself and others. """ 98 pass

 实例:寻找差异

# 数据库中原有
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 }
}

交集、差集

old_dict.keys()

new_dict.keys()

交集:要更新的数据

差集:

  原来,要更新

old = set(old_dict.keys())

new = set(new_dict.keys())

update_set = old.intersection(new)

delete_set = old.symmetric_difference(update_set)

add_set = new.symmetric_difference(update_set)

 

#交集(更新)

update_set = old.intersection(new)

 

#差集

difference 获取自己没有的、如果update_set 比它多、就没办法准备得到结果

deleta_set = old.difference(update_set)

for item in old:

  if item in update_set:

    del "old.item"

 

add_set = new.difference(update_set)

 

#对称差

delete_set = old.symmetric_difference(update_set)

add_set = new.symmetric_difference(update_set)

例:
s1 = set([11,22,33]) s2 = set([22,44]) ret1 = s1.difference(s2) ret2 = s1.symmetric_difference(s2) print(ret1) print(ret2)

得:

{33, 11}
{33, 11, 44}

 

#差集

delete_set = old.difference(new)

add_set = new.symmetric_difference(update_set)

 

 

 

#数据库中原有
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': 'c1', 'cpu_count': 2, 'mem_capicity': 80}
}
old_set = set(old_dict.keys())#把old_dict的键转换成set
update_list = list(old_set.intersection(new_dict.keys()))#取交集、创建一个新的set、最后得到一个新的列表、这是要 更新

new_list = []
del_list = []

for i in  new_dict.keys():
    if i not in update_list:
        new_list.append(i)#判断新字典里i是否在update_list(要删除的里面),如果不在添加到new_list列表里面这是需要新建

for i in old_dict.keys():
    if i not in update_list:
        del_list.append(i)#判断旧字典i是否在update_list(要删除的里面),如果不在添加到del_list里列表里面这是需要删除

print(update_list,new_list,del_list)

得:
['#3', '#1'] ['#4'] ['#2']
原文地址:https://www.cnblogs.com/mrzuo/p/7089037.html