####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‘]
去重
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‘}]
(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))
pythons = {"tom","jack","alex","egon","lxx"}
linuxs = {"lili","hanmeimei","张三","egon","lxx","jay"}
print(pythons & linuxs)
print(pythons.intersection(linuxs))
pythons = pythons & linuxs
pythons.intersection_update(linuxs)
print(pythons | linuxs)
print(pythons.union(linuxs))
只学了python的学生姓名
print(pythons - linuxs)
print(pythons.difference(linuxs))
只学了linux的学生姓名
print(linuxs - pythons)
res = (pythons - linuxs) | (linuxs - pythons)
res = pythons ^ linuxs
print(res)
print(pythons.symmetric_difference(linuxs))
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)
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()
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、什么是文件
文件是操作系统提供给用户或者应用程序操作硬盘的一种机制
2、为何要用文件
读写文件----->存取硬盘
应用程序: open()
操作系统: 打开文件
计算机硬件: 硬盘空间
3、如何用文件
打开文件
读/写
关闭文件
f = open
f = open(r"a\a.txt",mode="rt",)
f的值-》文件句柄,文件对象
res = f.read()
print(res,type(res))
f.close() # 回收操作系统的资源
print(f)
f.read()
with open(r"a\a.txt",mode="rt",) as f,
open(r"a\b.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)
原文:https://www.cnblogs.com/liuyang521/p/14185908.html