![077be0634b9c5cd71306c910a3bff53f.png](https://i-blog.csdnimg.cn/blog_migrate/1c4d98e04a646d755a8a950b139f5a01.png)
昨日补充:
####d.update():更新,无就添加,有就覆盖
d={"name":"egon","age":18,"gender":"xxxx"}
res = d.update({"gender":"male"})
print(d)>>>>>{'name': 'egon', 'age': 18, 'gender': 'male'}
print(res)>>>>None
####d.setdefault():有就不变,无才添加
res = d.setdefault("gender","male")
print(res)>>>>>>>xxxx``
print(d)>>>>>{'name': 'egon', 'age': 18, 'gender': 'xxxx'}
取交集(麻烦的办法)
pythons = ["tom","jack","alex","egon","lxx"]
linuxs = ["lili","hanmeimei","张三","egon","lxx","jay"]
l=[]
for name in pythons:
if name in linuxs:
l.append(name)
print(l)>>>>>['egon', 'lxx']
集合类型set基本使用
1、用途:
去重
(1) 无法保证顺序
msg = "hello"
res=set(msg)>>>>{'0','l','e','h'} 无顺序
print(''.join(res))>>>>oleh
names = ['egon','lxx','lili',"egon","egon"]
print(list(set(names)))>>>>>>['egon', 'lxx', 'lili'] 去重
(2)只能针对不可变元素的去重
students_info = [
{"name":"egon","age":18,"gender":"male"},
{"name":"tom","age":19,"gender":"male"},
{"name":"jack","age":38,"gender":"female"},
{"name":"egon","age":18,"gender":"male"},
{"name":"egon","age":18,"gender":"male"},
]
new_l = []
for info in students_info:
if info not in new_l:
new_l.append(info)
print(new_l)>>>>>[{'name': 'egon', 'age': 18, 'gender': 'male'},
{'name': 'tom', 'age': 19, 'gender': 'male'},
{'name': 'jack', 'age': 38, 'gender': 'female'}]
关系运算
2、定义方式: 在{}内用逗号分隔开多个元素,集合内元素的特点有三个
(1)集合内元素必须是不可变类型
(2)集合内元素不能重复
(3)集合内元素无序
s = {11,3.1,"xxx",(1,2,3),}
s = {11,11,11,11,11,22,33} # s = set(...)
print(s)
print(type(s))
数据类型转换
res = set("hello")>>>>>字符串转集合
print(res)
s = set()
print(s,type(s))
3、常用操作+内置的方法
pythons = {"tom","jack","alex","egon","lxx"}
linuxs = {"lili","hanmeimei","张三","egon","lxx","jay"}
(1)交集:取两个集合的共同部分->既报名pythons又报名linuxs的学生
print(pythons & linuxs)
print(pythons.intersection(linuxs))
pythons = pythons & linuxs
pythons.intersection_update(linuxs)
(2)并集:两个集合并到一起->所有学生的姓名
print(pythons | linuxs)
print(pythons.union(linuxs))
(3)差集: 一个集合减掉与另外一个集合共同的部分
只学了python的学生姓名
print(pythons - linuxs)
print(pythons.difference(linuxs))
只学了linux的学生姓名
print(linuxs - pythons)
(4) 交叉补集/对称差集:两个集合互相减,然后再并到一起-》取没有同时学两门课程的学生
res = (pythons - linuxs) | (linuxs - pythons)
res = pythons ^ linuxs
print(res)
print(pythons.symmetric_difference(linuxs))
(5)父子集合:当一个集合完全包含了另外一个集合,该集合才能称爹
s1 = {1,2,3}
s2 = {3,4,5}
print(s1 >= s2)
print(s2 <= s1)
s1 = {1,2,3}
s2 = {1,2}
print(s1 >= s2)
print(s1.issuperset(s2))
print(s2 <= s1)
print(s2.issubset(s1))
需要掌握的
s1 = {1,2,3}
1、s1.update()
s1.update({3,4,5})
print(s1)
2、s1.add()
s1.add(4)
print(s1)
3、删除
s1 = {11,22,33,44,55,"xx","yy","zz","aa"}
s1.remove(3)
print(s1)
s1.remove(333333333)
s1.discard(333333333)
print(s1)
res=s1.pop()
print(s1)
print(res)
s1.clear()
print(s1)
s1 = {11,22,33,44,55,"xx","yy","zz","aa"}
s1.copy()
4、s1.isdisjoint()判断是否是无交集
s1 = {1,2,3}
s2 = {3,4,5,6}
print(s1.isdisjoint(s2))
==该类型总结
存多个值
无序
可变
s1 = {1,2,3}
print(id(s1))
s1.add(4)
print(id(s1)) 不变
==文件处理(1)
致命三连击!!!
1、什么是文件
文件是操作系统提供给用户或者应用程序操作硬盘的一种机制
2、为何要用文件
读写文件----->存取硬盘
应用程序: open()
操作系统: 打开文件
计算机硬件: 硬盘空间
3、如何用文件
打开文件
读/写
关闭文件
![c94e518113b2e3089d83e3a165072be7.png](https://i-blog.csdnimg.cn/blog_migrate/a93dee12928ea71ff958592780ff99d7.jpeg)
f = open
f = open(r"aa.txt",mode="rt",)
f的值-》文件句柄,文件对象
res = f.read()
print(res,type(res))
f.close() # 回收操作系统的资源
print(f)
f.read()
with 上下文管理,子代码执行完毕可自动回收操作系统资源
with open(r"aa.txt",mode="rt",) as f,
open(r"ab.txt",mode="rt",) as f1:
res = f.read()
with open(r"a.txt",mode="rt",) as f:
for line in f:
# print(line)
res=line.strip('n').split(':')
print(res)