字典相关函数/集合相关的操作 (交差并补)
一 字典相关函数
增
dic = {} dic["top"] = "廉颇" dic["middle"] = "妲己" dic["bottom"] = "孙尚香" print(dic)
1.fromkeys() 使用一组键和默认值创建字典 (快速定义字典)
lst = ["a","b",'c'] # fromkeys(塞满键的容器,默认值) dic = {}.fromkeys(lst,None) print(dic) # {'a': None, 'b': None, 'c': None}
删
1.pop() 通过键去删除键值对 (若没有该键可设置默认值,预防报错)
dic = {'top': '廉颇', 'middle': '妲己', 'bottom': '孙尚香',"jungle":"猴子","support":"小明"} # 指定键删除对应的键值对 res = dic.pop("jungle") # 若没有该键可设置默认值,预防报错 res = dic.pop("middle123434","抱歉,该键不存在") print(res) print(dic)
2.popitem() 删除最后一个键值对
dic = {'top': '廉颇', 'middle': '妲己', 'bottom': '孙尚香',"jungle":"猴子","support":"小明"} res = dic.popitem() print(res) print(dic)
3.clear() 清空字典
dic.clear() print(dic)
改
update() 批量更新(有该键就更新,没该键就添加)
dic = {'top': '廉颇', 'middle': '妲己', 'bottom': '孙尚香'} # 没该键就添加 dic_new = {"support":"瑶"} dic.update(dic_new) print(dic) # 有该键就更新 dic_new = {'middle': '武则天'} dic.update(dic_new) print(dic) # 额外的用法(了解) dic.update(middle="甄姬",jungle="刘备") print(dic)
查
get() 通过键获取值(若没有该键可设置默认值,预防报错)
dic = {'top': '廉颇', 'middle': '妲己', 'bottom': '孙尚香'} res = dic.get("top123") res = dic.get("top123","抱歉,改键不存在") # res = dic["top"] print(res)
其他相关方法
#keys() 将字典的键组成新的可迭代对象 dic = {'top': '廉颇', 'middle': '妲己', 'bottom': '孙尚香'} res = dic.keys() print(res , type(res)) #values() 将字典中的值组成新的可迭代对象 res = dic.values() print(res , type(res)) #items() 将字典的键值对凑成一个个元组,组成新的可迭代对象 res = dic.items() print(res , type(res)) """ dict_items( [ ('top', '廉颇'), ('middle', '妲己'), ('bottom', '孙尚香')] ) <class 'dict_items'> """ for k,v in dic.items(): print(k,v)
二. 集合相关的操作(交叉并补)
set1 = {"郭杰瑞","药水哥","张大仙","王文"}
set2 = {"蔡徐坤","王力宏","郭德纲","王文"}
1.交集
res = set1.intersection(set2) print(res) # 简写 res = set1 & set2 print(res)
2.差集
res = set1.difference(set2) print(res) # 简写 res = set1 - set2 print(res)
3.union 并集
res = set1.union(set2) print(res) # 简写 res = set1 | set2 print(res)
4.symmetric_difference
res = set1.symmetric_difference(set2) print(res) # 简写 res = set1 ^ set2 print(res)
set1 = {"王文","王宝强","王健林","王思聪"}
set2 = {"王文","王宝强"}
5.issubset() 判断是否是子集
res = set2.issubset(set1) print(res)
# 简写
res = set2 < set1
print(res)
6. issuperset() 判断是否是父集
res = set1.issuperset(set2) print(res) # 简写 res = set1 > set2 print(res)
7.isdisjoint() 检测两集合是否不相交 不相交 True 相交False
set1 = {"王文","王宝强","王健林","王思聪"} set2 = {"王文","王宝强"} res = set1.isdisjoint(set2) print(res)
集合相关的方法
增
setvar = {"周润发"} # 1.一次加一个 #add() 向集合中添加数据 setvar.add("周星驰") setvar.add("周树人") print(setvar) # 2.一次加一堆 #update() 迭代着增加 setvar = {"马诺"} lst = ["马蓉","马化腾","马户","马云","码农"] lst = "abc" setvar.update(lst) print(setvar)
删
setvar = {'张大仙', '郭杰瑞', '药水哥', '郭德纲', '蔡徐坤', '王力宏'}
1clear() 清空集合
# setvar.clear() # print(setvar)
2. pop() 随机删除集合中的一个数据
# res = setvar.pop() # print(res) # print(setvar)
3. discard() 删除集合中指定的值(不存在的不删除 推荐使用)
setvar.discard("郭杰瑞") setvar.discard("郭杰瑞11122333") print(setvar)
冰冻集合 (扩展)
"""冰冻集合一旦创建,不能在进行任何修改,只能做交叉并补操作""" # frozenset 可强转容器类型数据变为冰冻集合 lst = ["马蓉","马化腾","马户","马云","码农"] fz = frozenset(lst) print(fz,type(fz)) fz1 = frozenset( {"马蓉","马化腾"} ) fz2 = frozenset( {"马化腾","马户"} ) # 只能做交叉并补操作 res = fz1 & fz2 print(res) # 可以做添加操作么? 不可以 fz1.add("abc")