python的学习之路(三)

2019-05-10 06:00:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

一、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)



原文链接:https://www.cnblogs.com/harsonlee/p/10832993.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:day21 05 员工信息表

下一篇:django 使用框架下auth.models自带的User进行扩展增加字段