day12:元祖的操作、字典的操作、文件操作、pickle模块

一、元祖遍历

使用for循环遍历

t = (1,2,3,4,5)
for var in t:
    print(var)
i = 0
t = ((1,2),(3,4),(5,6))
for var in t:
    for val in var:
        print(val)

使用while循环遍历

while i < len(t):
     print(t[i])
     i = i+1

元祖的+、*

+两个元祖合并,生成新元祖
*一个元祖重复输出,生成新元祖

t = (1,2,3,4,5)
t1 = (6,7)
t2 = t + t1
print(t2) #元祖不能改变
t2 = t * 3
print(t2)

二、元祖常用方法

由于元祖不能改变,所以其方法也少

1、查询结果 index

输入下标,返回元素

#查询结果
res = t.index(2)
print(res)

2、查询数量 count

查询输入元素的出现次数

#查询数量
res = t.count(4)
print(res)

3、元祖推导式

产生生成器,可以使用for循环遍历

t = (1,2,3,4,5)
res = (var**2 for var in t)
print(res)      #generator产生生成器
for var in res: #可以通过for、next遍历
    print(var)

三、字典的操作

字典的创建

dict1 = {"a":1,"b":2,"c":3}
print(dict1,type(dict1))

dict2 = dict([["a",1],["b",2]])#通过转换实现,少用
print(dict2,type(dict2))

dict3 = dict({"a":1,"b":2,"c":3})#转换,把字典转换成字典
print(dict3,type(dict3))

dict4 = dict(a=1, b=2, c=3)
print(dict4,type(dict4))

dict5 = dict(zip(("a","b","e","c"),(1,2,3)))#长度不同,以最短的为主
print(dict5,type(dict5))

字典的遍历

for key,value in dict5.items():
    print(key,value)

四、字典的内置方法

1、items 字典转化为类似元祖的方式

2、keys 将字典所有key组合成一个序列

3、values 将字典所有value组合成一个序列**

4、复制 copy

copy是浅拷贝,当二级字典发生变化是,拷贝的也会改变

#复制copy
dict1 = {"1":{"a":2,"b":3,"c":4}}
#复制copy
dict2 = dict1.copy()
print(dict2)
dict1["1"]["d"] = 1 #字典添加
print(dict2)        #在二级字典里的会随之改变
print(dict1)

5、get 根据key,返回value

好处是没有的key,不会报错,而是返回none

#get 根据key,获取value
dict1 = {"a":1,"b":2,"c":3}
#res = dict1[1] #如果这里搜索没有的key,报错导致程序结束
res = dict1.get("ab")
print(res) #则返回none

6、pop 移除指定元素,返回键对应值,popitem() 删除最后一个,字典为空,则报错

#pop 移除指定元素,返回键对应值
res = dict1.pop("a","key is no in here")#加上默认值则可以不用报错
print(res)
print(dict1)
#popitem() 删除最后一个,字典为空,则报错
dict1 = {"a":1,"b":2,"c":3}
res = dict1.popitem()
print(res)
print(dict1)

7、setdefault 添加一个键,如果存在则不进行操作

#setdefault 添加一个键,如果存在则不进行操作
dict1.setdefault("cc",7)
print(dict1)

8、update 没有的键值会添加,已有的会进行更新

#update
dict1.update({"jj":"hh"})
print(dict1)
dict2 = {"d":5,"e":5,"ff":7}
dict1.update(dict2)
print(dict1)    #没有的键值会添加,已有的会进行更新

五、文件操作

open(file_path,mode)
mode 权限:

w(write 写,如果没有这个人间会自动创建一个)

f = open("user.txt","w")
f.write("www.python.com")
f.close()       #写完之后记得关闭

r(read 读:read读取全部、readline读取一行、readlines以行为单位全部读取

f = open("user.txt","r")
#content = f.read()
#content = f.readline()
content = f.readlines() #最常用,以行为单位全部读取
print(content)
f.close()

a(append 追加,如果没有这个文件则创建一个,如果有则在文件末尾追加)

f = open("user.txt","a")
f.write("hello")
f.close()

w+ 读写权限 会覆盖之前内容(先清空再覆盖

r+ 读写 (不会先清空再覆盖,即没有创建新文件的权限

a+ 读写 (不会清除文件,而是在文件尾部追加)

f = open("user.txt","w+")
f.write("hello world")
f.seek(0,0)  #写操作之后光标会在最末端,导致读取从最末端开始,seek移动光标
content = f.read()
print(content)
f.close()
f = open("user.txt","r+")
f.write("world")
f.seek(0,0)  #写操作之后光标会在最末端,导致读取从最末端开始,seek移动光标
content = f.read()
print(content)
f.close()
f = open("user.txt","a+")
f.write("world")
f.seek(0,0)  #写操作之后光标会在最末端,导致读取从最末端开始,seek移动光标
content = f.read()
print(content)
f.close()
"""
输出结果为
hello world
world world
world worldworld
"""

wb 二进制写

rb 二进制读

ab 二进制追加写

#二进制模式一般在计算机交互时使用
f = open("user.txt","wb")
f.write("我好好学习".encode("utf-8"))
f.close()

f = open("user.txt","rb")
content = f.read().decode("utf-8")
print(content)
f.close()

#通过使用errors参数可以不报错,但是会是乱码
f = open("user.txt","w",encoding="utf-8")# binary mode doesn't take an encoding argument
f.write("我好好学习")
f.close()
f = open("user.txt","r",encoding="gbk",errors="ignore")
content = f.read()
print(content)
f.close()

为什么要close

在write的时候,是写到缓冲区中,而不是直接写进文件,
并且,文件不去close会导致文件没有保存,既没有生成

刷新缓冲区的方式:(即把缓冲区的内容写入文件中)
1、缓冲区呗占满了
2、关闭文件时自动刷新
3、程序运行结束
4、手动刷新缓冲区 flush()

with语法,不用手动关闭文件,执行结束,自动关闭文件

#with语法,不用手动关闭文件,执行结束,自动关闭文件
with open("user.txt","w",encoding="utf-8") as f:
    f.write("这是一个测试")
with open("user.txt","r",encoding="utf-8") as f:
    content = f.read()
    print(content)

六、pickle模块

使用前需要导入 import pickle
四个函数:
dump (文件用)
load
dumps (列表等使用)
loads
通过使用pickle模块可以方便二进制的数据传输解码

import pickle #是一个模块需要导入
L = [1,2,3,4,5]
res = pickle.dumps(L)#当网络发送时,转换成二进制发送
print(res)
con = pickle.loads(res)
print(con)  #接收时解码

#dump是用于文件操作的
with open("user.txt","wb") as f:
    str = "这是一个测试"
    pickle.dump(str,f)
with open("user.txt","rb") as f:
    res = pickle.load(f)
    print(res)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值