欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python的学习之路(三)

程序员文章站 2024-03-30 21:45:03
一、set集合#!/usr/bin/env python# *_*coding:utf-8 *_*# Author: harsonold_dict = { "#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80}, "#2": {'ho ......
一、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)