今天的学习以代码的形式开展:
##字典相关函数
dic = {}
#增
#1.普通方法
dic["top"] = "369"
dic["mid"] = "left"
print(dic)
#2.formkeys使用一组键和默认值创建字典
tup = ("a","b","c")
#fromkeys (盛放键的容器,默认值)
dic={}.fromkeys(tup,None)
print(dic)
#注意点 字典中的三个键默认指向史一个列表
#删
dic = {'top': '369', 'mid': 'left',"bottom":"jacklove"}
res = dic.pop("mid")
print(res)
print(dic)
#可以给pop设置第二个参数 , 以防止键不存在时报错
res = dic.pop("adc","不存在")
print(res)
#popitem() 删除最后一个键值对
dic = {'top': '369', 'mid': 'left',"bottom":"jacklove"}
res = dic.popitem()
print(res)
print(dic)
#clear 清空字典
dic.clear()
print(dic)
#改
#updata 批量更新,有就更新,没键就添加
#没键添加
dic = {'top': '369', 'mid': 'left',"bottom":"jacklove"}
dic_new = {"jungle":"ning","support":"baolan"}
dic.update(dic_new)
print(dic)
#有就更新
dic_new = {"top":"the shy","xiaochang":"王思聪"}
dic.update(dic_new)
#了解
dic.update(ww="王文")
print(dic)
#get 通过键获取值(若没有改建可设置默认值,预防报错)
#get 在获取字典键时,如果不存在。不会报错,返回none
dic_new = {"top":"the shy","xiaochang":"王思聪"}
res = dic["top"]
res = dic.get("top")
print(res)
#给与默认值提示
res = dic.get("123","不存在")
print(res)
#keys()获取键
dic = {"top":"the shy","xiaochang":"王思聪"}
res = dic.keys()
print(res,type(res))
for i in res:
print(i)
#values()获取值
res = dic.values()
print(res,type(res))
#item() 获取键和值
res = dic.items()
print(res,type(res))
for i in res:
print(i)
for k,v in res:
print(k,v)
#集合相关操作(交,差,并,补)
#intersection 交集
set1 = {"旭旭宝宝","pdd","五五开","云烨"}
set2 = {"刘备","关羽","张飞","马超","云烨"}
res = set1.intersection(set2)
print(res)
#简写 &
res = set1 & set2
print(res)
#差集
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 = set1.issubset(set2)
print(res)
#简写
res = set2 <= set1
print(res)
#issuperset 是否是父集
set1 = {"刘德华","郭富城","张学友","郭富城","云烨"}
set2 = {"云烨"}
res = set1.issuperset(set2)
print(res)
#简写
res = set1>set2
print(res)
#isdisjoint 检测两集合是否不相交 不相交ture 相交false
set1 = {"刘德华","郭富城","张学友","郭富城","云烨"}
set2 = {"云烨"}
res = set1.isdisjoint(set2)
print(res)
##集合相关函数
#增
#add 一次加一个 数据
set1 = {"云烨"}
set1.add("柳月")
print(set1)
#updata()迭代增加 一次加一堆
set1 = {"云烨"}
lst = ["a","b"]
set1.update(lst)
print(set1)
#删
#clear 清空
setvar = {"刘德华","郭富城","张学友","郭富城","云烨"}
setvar.clear()
print(setvar)
#pop 随机删除一个
setvar = {"刘德华","郭富城","张学友","郭富城","云烨"}
res = setvar.pop()
print(res)
print(setvar)
#discard 删除集合中指定的值(不存在不删除)
setvar = {"刘德华","郭富城","张学友","郭富城","云烨"}
setvar.discard("1")
print(setvar)
#remove 删除集合中指定的值(不存在报错)
setvar = {"刘德华","郭富城","张学友","郭富城","云烨"}
#冰冻集合
#frozenset 单纯只能做交差并补操作,不能做添加或者删除的操作
lst = ["1","2","3"]
fzl = frozenset(lst)
print(fzl)
#fp = open(文件,模式,编码集) fp => 文件的io对象(文件句柄)‘
#i => input 输入 o => outpur 输出
#fp.read()读取文件内容 fp.write()写入文件的内容
# 1.文件的写入操作
# 2.文件的读取操作
# (1)打开文件
# (2) 读取内容 res = fp.read()
# (3) 关闭文件 fp.close () print(res)
#文件模式
#打开
fp = open("1.txt",mode="w",encoding="utf-8")
#写入
fp.write("111")
#关闭
fp.close()
fp = open("1.txt",mode="r",encoding="utf-8")
res = fp.read()
fp.close()
#二进制字节流
#b b开头的字节流只能是ascii编码中的字符不能是中文
#encode 编码 将字符串编程字节流
#decode 解码 将字节流转换成字符串
data = b"abc"
print(data,type(data))
data = "中文".encode("utf-8")
print(data,type(data))
res =data.decode("utf-8")
print(res)
#utf-8 一个中文 3个字节
data = "中文".encode("utf-8")
print(len(data))
#中文字节流进行反解
res = b'\xe4\xb8\xad\xe6\x96\x87'.decode()
print(res)
#文件存储二进制的字节流
"""如果存储二进制字节流,指定模式wb,不指定encoding编码集,否则报错"""
fp = open("2.txt",mode="wb")
strvar = "云烨".encode()
fp.write(strvar)
fp.close()
#复制文件集合.png
#所有的图片、音频、视频都需要通过二进制来进行存储
#先把源文件子文件字节流读取出来
#相对路径
fp = open("1.png",mode ="rb")
#绝对路径
fp = open(r"D:\Cloud\Python\day_01\1.png",mode= "rb")
res = fp.read()
fp.close()
#计算文件中的字节个数
print(len(res))
#再把二进制字节流写到另一个文件中,相当于复制
fp = open("2.png",mode="wb")
fp.write(res)
fp.close()
#utf-8编码中 一个中文3个字节,一个英文或符号一个字节
#read() 读取字符个数 当前光标右读
#seek 调整指针位置 seek(0) 移动到0字节位置
# seek(0,2) 移动到文件末尾
#tell 当前光标左侧字节数
#文件操作的扩展模式
#r+先读后写
#先读
fp = open("3.txt",mode="r+",encoding="utf-8")
res = fp.read()
print(res)
#在写
fp.write("ab")
#再读
res = fp.read()
print(res)
fp.close()
#先写后读
fp = fp = open("3.txt",mode="r+",encoding="utf-8")
#移动光标到最后,否则r模式下,原字符被覆盖
fp.seek(0,2)
fp.write("cd")
#再读
fp.seek(0)
res = fp.read()
fp.close()
#3 w+ 可读可写,清空重写 默认可以创建新的文件
fp = open("4.txt",mode="w+",encoding="utf-8")
fp.write("abc")
fp.seek(0)
print(fp.read())
fp.close()
#4 a+ 可读可写 ,追加写入
# append
fp = open("5.txt",mode="a+",encoding="utf-8")
fp.write("def")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
#5.r+和a+区别
"""r+模式基于当前光标位置进行写入覆盖
a+模式会强制把光标放到文件末尾进行追加
读模式没有区别
"""
fp = open("5.txt",mode="a+",encoding="utf-8")
fp.seek(3)#第三个字节的位置
fp.write("zxc")
fp.close()
fp = open("5.txt",mode="r+",encoding="utf-8")
fp.seek(3)#第三个字节的位置
fp.write("zxc")
fp.close()
#6.seek tell read 之间的使用
fp = open("5.txt",mode="r+",encoding="utf-8")
fp.seek(4)
#telpp 当前光标左边所有内容的字节数
res = fp.tell()
print(res)
#在r+模式下 read(2)代表读取两个字符,在rb模式下read(2)代表读取两个字节
#7.注意点(seek 在移动到某个汉字的字节中间,导致字节无法解析)
fp = open("6.txt",mode="r+",encoding="utf-8")
fp.seek(3)
fp.close()
#8.with 语句 自动实现文件关闭操作 as 起别名
with open("1.png",mode ="rb") as fp:
res = fp.read()
with open("2.png",mode ="wb") as fp:
fp.write(res)
#继续简化
with open("1.png",mode ="rb") as fp1,open("2.png",mode ="wb") as fp2:
res = fp1.read()
fp2.write(res)