day3-set集合

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


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
# 数据库中原有
c1 = "a"
c2 = "b"
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 }
}
 
 
s_old = set(old_dict.keys())
s_new = set(new_dict.keys())
 
 
update_set = s_old.intersection(s_new)
print(update_set)
{'#3', '#1'}
 
delete_set = s_old.difference(s_new)
print(delete_set)
{'#2'}
 
add_set = s_new.difference(update_set)
print(add_set)
{'#4'}







原文地址:https://www.cnblogs.com/bruceg/p/5150363.html