码迷,mamicode.com
首页 > 编程语言 > 详细

python集合set

时间:2016-08-15 17:24:22      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

 set 交集、差集

在新旧字典中,新旧字典均存在的KEY进行更新。旧字典存在而新字典不存在,则删除。旧字典不存在而新字典存在的KEY,则新增;

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}
}
new_dict = {
    "#1": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80},
    "#3": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80},
    "#4": {‘hostname‘: ‘c1‘, ‘cpu_count‘: 2, ‘mem_capicity‘: 80}
}
# 转换成set集合
old = set(old_dict)
new = set(new_dict)

# 交集
update_set = new.intersection(old)
# 差集
delete_set = old.symmetric_difference(update_set)
# 差集
add_set = new.symmetric_difference(update_set)

print update_set
print delete_set
print add_set

  

python集合set

标签:

原文地址:http://www.cnblogs.com/mjoy/p/5773466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!