day07-Python运维开发基础(深/浅拷贝、字典/集合/相关操作)
1. 深拷贝与浅拷贝
# ### 深拷贝 和 浅拷贝 """ a = 7 b = a a = 8 print(b) lst1 = [1,2,3] lst2 = lst1 lst1.append(4) print(lst2)day """ # 浅拷贝 copy模块中有一个同名方法也叫copy # 方法一 (推荐) """ import copy lst1 = [1,2,3] lst2 = copy.copy(lst1) lst1.append(4) print(lst2) """ # 方法二 # 列表.copy lst1 = [1,2,3] lst2 = lst1.copy() lst1.append(5) print(lst1) print(lst2) # 深拷贝 copy模块中有一个方法叫deepcopy """ # 浅拷贝只能复制列表中的一级所有元素,二级或者多级中的元素无法复制,所以引出深拷贝 lst1 = [1,2,3,[4,5,6,7]] lst2 = lst1.copy() lst1[-1].append(7) print(lst2) """ import copy lst1 = [1,2,3,[4,5,6]] lst2 = copy.deepcopy(lst1) lst1[-1].append(9) print(lst2) # 深拷贝字典 dic1 = {"a":1 , "b":[1,2,3]} dic2 = copy.deepcopy(dic1) dic1["b"].insert(2,4) print(dic2) print(dic1) # 深拷贝 比 浅拷贝 占用的空间更大,速度稍慢; # 多级容器用深拷贝, 一级容器用浅拷贝
2. 字典相关函数
# ### 字典相关函数 dic = {} # "top":"the shy","middle":"faker","bottom":"uzi","support":"rookie","jungle":"厂长" # 增 dic["top"] = "the shy" dic["middle"] = "faker" dic["bottom"] = "uzi" dic["support"] = "mata" dic["jungle"] = "kakao" print(dic) #fromkeys() 使用一组键和默认值创建字典 (为当前键赋初始值,不推荐) lst = ["top","middle","bottom"] dic = {}.fromkeys(lst,None) print(dic) # 注意点(列表) lst = ["top","middle","bottom"] dic = {}.fromkeys(lst,[]) print(dic) dic["top"].append(1) print(dic) # 删 dic = {'top': 'the shy', 'middle': '西门', 'bottom': 'uzi', 'support': 'mata', 'jungle': 'kakao'} #pop() 通过键去删除键值对 (若没有该键可设置默认值,预防报错) # res = dic.pop("middle") # print(dic) # print(res) mykey = "jungle123" res = dic.pop(mykey,"该键不存在") print(res , dic) #popitem() 删除最后一个键值对 res = dic.popitem() print(res) print(dic) #clear() 清空字典 dic.clear() print(dic) # 改 #update() 批量更新(有该键就更新,没该键就添加) dic = {"ldb":"吕洞宾","lch":"蓝采和","tgl":"忒乖离","zgl":"张果老","xboyww":"神秘男孩"} dicnew = {"hxg":"何仙姑","xboyww":"王文"} dic.update(dicnew) print(dic) # 查 #get() 通过键获取值(若没有该键可设置默认值,预防报错) dic = {"ldb":"吕洞宾","lch":"蓝采和","tgl":"忒乖离","zgl":"张果老","xboyww":"神秘男孩"} # print(dic["xboyww1111111"]) # 如果正常进行获取,当键不存在时直接报错 print(dic.get("xboyww1111111","该键不存在")) # get方法可以在不存在这个键时,设置默认值,不报错 dic = {"ldb":"吕洞宾","lch":"蓝采和","tgl":"忒乖离","zgl":"张果老","xboyww":"神秘男孩"} #keys() 将字典的键组成新的可迭代对象 res = dic.keys() print(res) #values() 将字典中的值组成新的可迭代对象 res = dic.values() print(res) #items() 将字典的键值对凑成一个个元组,组成新的可迭代对象 res = dic.items() print(res)
3. 集合相关操作与函数
# ### 集合的相关操作 set1 = {"郭富城","刘德华","张学友","王文"} set2 = {"王宝强","王源","王文"} #intersection() 交集 res = set1.intersection(set2) print(res) # 简写 & res = set1 & set2 print(res) # difference() 差集 res = set1.difference(set2) print(res) # 简写 - res = set1 - set2 print(res) #union() 并集 res = set1.union(set2) print(res) # 简写 | res = set1 | set2 print(res) # symmetric_difference 对称差集 res = set1.symmetric_difference(set2) print(res) # 简写 ^ res = set1 ^ set2 print(res) #issubset() 判断是否是子集 set1 = {"周杰伦","林俊杰","王文"} set2 = {"王文"} res = set2.issubset(set1) print(res) # 简写 < <= res = set2 < set1 print(res) #issuperset() 判断是否是父集 set1 = {"周杰伦","林俊杰","王文"} set2 = {"王文"} res = set1.issuperset(set2) print(res) # 简写 > >= res = set1 > set2 print(res) #isdisjoint() 检测两集合是否不相交 不相交 True 相交False set1 = {"周杰伦","林俊杰","王文"} set2 = {"王文"} res = set1.isdisjoint(set2) print(res)
# ### 集合的相关函数 setvar = {"王文"} # 增 #add 向集合中添加数据 setvar.add("周瑞发") print(setvar) #update() 迭代着增加 lst = ["a","b","c"] setvar.update(lst) print(setvar) # 删 #clear() 清空集合 setvar.clear() print(setvar) setvar = {"王文","王源","黄渤"} #remove() 删除集合中指定的值(不存在则报错) # setvar.remove("黄渤") # setvar.remove("黄渤1212") error 不存在则报错 print(setvar) #discard() 删除集合中指定的值(不存在的不删除 推荐使用) setvar.discard("黄渤1212") # 不报错,不会导致下面的代码终止 #pop() 随机删除集合中的一个数据 res = setvar.pop() print(res) print(setvar) # 冰冻集合 """冰冻集合一旦创建,不能在进行任何修改,只能做交叉并补操作""" # 定义一个空的冰冻集合 fz = frozenset() print(fz,type(fz)) lst1 = ["a","b","c","d"] lst2 = ["a","b","f","z"] fz1 = frozenset(lst1) print(fz1,type(fz1)) fz2 = frozenset(lst2) print(fz2,type(fz2)) # 交集 res = fz1 & fz2 print(res) # 差集 res = fz1 - fz2 print(res) # 不能在冰冻集合当中 做添加或者删除操作 # fz1.add("fff") error
day07