python的学习之路(三)

一、set集合
#!/usr/bin/env python
# *_*coding:utf-8 *_*
# Author: harson

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}
}
old = set(old_dict.keys())
new = set(new_dict.keys())

update_set = old.intersection(new)

del_set = old.symmetric_difference(update_set)
insert_set = new.symmetric_difference(update_set)

print(update_set)
print(del_set)
print(insert_set)
二、collection
#!/usr/bin/env python
# *_*coding:utf-8 *_*
# Author: harson

import collections

obj = collections.Counter('dsafd45f45d6s4t6we4g56ds4f5')
print(obj)
obj1 = obj.elements()
print(obj1)
for k in obj.elements():
print(k)
for k,v in obj.items():
print(k,v)

#有序字典
dic = collections.OrderedDict()
dic['k1'] = 'v1'
dic['k2'] = 'v2'
dic['k3'] = 'v3'
print(dic)
dic.move_to_end('k1')
print(dic)
dic.popitem()
print(dic)
dic.pop('k2')
print(dic)
dic.setdefault('k4','v4')
dic.update({'k1':'v111','k5':'v5'})
print(dic)

#默认字典
ddic = collections.defaultdict(list)
ddic['k1'].append('v123')
print(ddic)


猜你喜欢

转载自www.cnblogs.com/harsonlee/p/10832993.html
今日推荐