码迷,mamicode.com
首页 > 其他好文 > 详细

day3-set集合

时间:2016-01-22 11:00:57      阅读:105      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!