首页 > 其他 > 详细

day3-set集合

时间:2016-01-22 10:58:44      阅读:161      评论:0      收藏:0      [点我收藏+]

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‘}







day3-set集合

原文:http://www.cnblogs.com/bruceg/p/5150363.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!