首页 > 编程语言 > 详细

python集合set

时间:2016-08-15 17:23:34      阅读:210      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!