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
时间: 2024-10-05 22:51:41