python小白——进阶之路——day7天---字典和集合的相关函数

时间:2021-09-01 22:41:49
### 字典的相关函数(增删改查)
#(1)增
dictvar= {}
dictvar['top'] = "凯"
dictvar['middle'] = "妲己"
dictvar["bottom"] = "鲁班七号"
dictvar["jungle"] = "刘备"
dictvar["support"] = "刘邦"
print(dictvar)

#fromkeys()使用一组键和默认值创建字典
listvar = ["a","b"]
dic = {}.fromkeys(listvar,None)
print(dic)
#不推荐使用formkeys
'''
dic = {}.fromkeys(listvar,[])
dic["a"].append(55)
print(dic["b"])
'''
#(2)删除
#pop() 通过键去删除键值对(若没有改建可设置默认值,预防报错)
dictvar ={'top': '凯', 'middle': '妲己', 'bottom': '鲁班七号', 'jungle': '刘备', 'support': '刘邦'}
dictvar.pop("middle")
print(dictvar)
#dictvar.pop("middle123")
dictvar.pop("middle123","该键不存在")
print(dictvar)

# popitem()删除最后一个键值对 3.6版本
dictvar = {'top': '凯', 'middle': '妲己', 'bottom': '鲁班七号', 'jungle': '刘备', 'support': '刘邦'}
dictvar.popitem()
print(dictvar)

# clear() 清空字典
dictvar.clear()
print(dictvar)

#(3)改
#update()批量更新(有该键就有更新,没该键就添加)

dic = { 'jungle': '刘备', 'support': '刘邦'}
# dic.update({'top':"花木兰",'middle':"武则天"})
# 写法一 (推荐)
dic.update( {'top':"花木兰",'middle':"武则天","support":"蔡文姬"} )
print(dic)
#写法二
dic.update(abc = "123",bbb=456,ccc=999)
print(dic)

#(4)查
#get()通过键值对(若没有该键可设置默认值,预防报错)
dictvar = {'top': '凯', 'middle': '妲己', 'bottom': '鲁班七号', 'jungle': '刘备', 'support': '刘邦'}
# dictvar["top123"] error
res = dictvar.get("top123")
#r如果没有该键,默认返回None,如果指定默认值,则返回该默认值
res = dictvar.get("top")
print(res)
print("===================================================>>>>>>>>>>>>>>>>>>>>>>>>>>.")
#重点记忆:
#keys()将字典的键组成新的可迭代对象
dictvar = {'top': '凯', 'middle': '妲己', 'bottom': '鲁班七号', 'jungle': '刘备', 'support': '刘邦'}
for i in dictvar:
print(i)
#能够遍历就证明具有可迭代性
res = dictvar.keys()
print(res)

for i in res:
print(i)
#values()将字典中的值组成新的可迭代对象
print("=============================================")
res =dictvar.values()
print(res)
for i in res:
print(i)
print("===============================================")
#items()将字典的键值对凑成一个个元组,组成新的可迭代对象
res = dictvar.items()
print(res)
for i in res:
print(i)
for a,b in res:
print(a,b)




###集合相关操作(交差并补)
#intersection()交集
set1 = {"周杰伦","王文","刘德华"}
set2 = {"周润发","周星星","王文"}
res = set.intersection(set2)
print(res)
res = set1 & set2
print(res)

#difference() 差集
set1= {"方贵权","黄一真","黄少正","黄熊大","皇阿玛"}
set2 = {"邓远文","王俊文","黄文","黄熊大"}
res = set1.difference(set2)
print(res)
res = set1 - set2
print(res)
#union() 并集
set1= {"方贵权","黄一真","黄少正","黄熊大","皇阿玛"}
set2 = {"邓远文","王俊文","黄文","黄熊大"}
res =set1.union(set2)
print(res)
res = set1 | set2
print(res)
#symmetric_difference() 对称差集 (补集情况涵盖在其中)
set1= {"方贵权","黄一真","黄少正","黄熊大","皇阿玛"}
set2 = {"邓远文","王俊文","黄文","黄熊大"}
res =set1.symmetric_difference(set2)
print(res)
res = set1 ^ set2
print(res)
#issubset() 判断是否是子集
set1 = {"王文","王俊文","黄文"}
# set2 = {"王文","王俊文","黄文"}
set2 = {"黄文"}
res = set2.issuperset(set1)
print(res)
#真子集:2个集合不是完全一样的
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)


###集合相关函数
#(1)增
#add() 向集合中添加数据
setvar = {"王文","王俊文","黄文"}
setvar.add("张耀文")
print(setvar)
#update()迭代者增加 update(可迭代数据)
setvar.update(["陈文江","姜文"])
print(setvar)
#(2)删除
#clear()清空集合
setvar = {"王文","王俊文","黄文"}
setvar.clear()
print(setvar)
#pop 随机删除集合中的一个数据
setvar ={"王文","王俊文","黄文"}
setvar.pop()
print(setvar)
#remove() 删除集合中指定的值(不存在则报错)
setvar = {"王文","王俊文","黄文"}
setvar.remove("黄文")
# setvar.remove("黄133文") error
print(setvar)
#discard() 删除集合中指定的值(不存在的不删除 推荐使用)
setvar = {"王文","王俊文","黄文"}
res = setvar.discard("黄133文")
print(setvar)
# print(res)

###冰冻集合(只能做交叉并补)
#frozenset 可强转容器类型数据变为冰冻集合
#冰冻集合一旦创建,不能再进行任何修改,只能做交叉并补操作

#(1)声明空的冰冻集合
fz1 = frozenset([1,2,3,4])
print(fz1,type(fz1))
# fz2 = frozenset("2345")
fz2 = frozenset((2,3,4,5))
print(fz2,type(fz2))

res = fz1 & fz2
print(res)

# fz1.add("890") # error 不允许添加或者删除操作.