1.集合的操作
1.集合的操作
定义:
1.不同元素组成,自动去重
2.无序
3.集合中的元素必须是不可变类型
1.集合的定义:
1>>> s1 = set('abcd') #同s1 = set{'a','b','c','d'}
2 >>>s13 {'a', 'c', 'b', 'd'}4 >>> t1 = set('cdef')5 >>>t16 {'c', 'f', 'e', 'd'}7 >>> un_set = set('hello')8 >>>un_set9 {'h', 'e', 'o', 'l'}10 >>> #set是去重的
11 >>> un_set = frozenset(un_set) #定义只读集合12 >>>un_set13 frozenset({'h', 'e', 'o', 'l'})14 >>> #定义只读集合
2.集合的运算:
集合之间也可进行数学集合运算(例如:并集、交集等),可用相应的操作符或方法来实现。
#子集
>>> s1 = set('abcd')>>> t1 = set('cdef')>>> C = set('ab')>>> C>s1 #判断C是否为S1子集,用符号表示
False>>> C
True>>> C.issubset(s1) #用方法.issubset()表示
True
#交集:所有的相同元素的集合
1 >>> t1 = set('cdef')2 >>> s1 = set('abcd')3 >>> i = s1 & t14 >>>i5 {'c', 'd'}6 >>> s1.intersection(t1)7 {'c', 'd'}
#并集:集合中所有元素的集合,重复元素只出现一次
1 >>> s1 |t1 #符号表示2 {'f', 'e', 'a', 'c', 'b', 'd'}3 >>>s1.union(t1) #方法表示4 {'f', 'e', 'a', 'c', 'b', 'd'}
#差集:s1和t1的差集,所有属于s1且不属于t1的元素集合
>>> s1 -t1
{'a', 'b'}>>>s1.difference(t1)
{'a', 'b'}
#对称差集,s1和t1去掉公共元素后,其余元素的集合
1 >>> s1 ^ t12 {'f', 'e', 'a', 'b'}3 >>> s1.symmetric_difference(t1)4 {'f', 'e', 'a', 'b'}
3.集合的方法
参考博客,非常详细:
1 #方法
2 >>> s = {1,2,3,4,5}3 >>>s.add('s') #增加元素
4 >>>s5 {'s', 1, 2, 3, 4, 5}6 >>>
7 >>> s.clear() #清空集合
8 >>>s9 set()10 >>>
11 >>> s = {1,2,3,4,5}12 >>>s13 {1, 2, 3, 4, 5}14 >>> new_s = s.copy() #浅copy
15 >>>new_s16 {1, 2, 3, 4, 5}17 >>>
18 >>>
19 >>> #pop()
20 >>> s.pop() #随机删除,若无,会报错 KeyError
21 1
22 >>>s23 {2, 3, 4, 5}24 >>>
25 >>>
26 >>> s.remove(2) #删除指定元素,若指定元素不存在,会报错 KeyError
27 >>>s28 {3, 4, 5}29 >>>
30 >>>s.discard("3") #删除集合中的一个元素,如果元素不存在,则不执行任何操作
31 >>>s32 {3, 4, 5}33 >>>
34 >>> s.discard(3)35 >>>s36 {4, 5}37 >>>
38
39 >>> #并集.union() 符号:| 所有元素的集合,重复元素只出现一次
40 >>> #交集 .intersection() 符号:& 所有相同元素的集合
41 >>> #子集.issubset() 符号:< 判断是否是子集
42 >>> #父集.isuperset() 符号:> 判断是否是父集
43 >>> #差集.difference() 符号:- 所有属于s1且不属于t1的元素构成新的set
44
45 >>> #对称差集 .symmetric_difference() 符号:^
46 #对称差集,去掉公共元素后,其余元素的集合
47
48
49
50 >>>s151 {'a', 'c', 'b', 'd'}52 >>>t153 {'c', 'f', 'e', 'd'}54 >>> s1.intersection_update(t1) #用s1与t1的交集,来替换为s1
55 >>>s156 {'c', 'd'}57
58
59 >>> s1 = {1,2}60 >>> t1 = {2,3,4}61 >>> s1.update(t1) #update
62 >>>s163 {1, 2, 3, 4}64 >>>
65 >>>s1.update(['h','i'],{4,5,6}) #方法中直接添加元素,可list和set
66 >>>s167 {1, 2, 3, 4, 'h', 5, 6, 'i'}68 >>>
69
70
71 >>>s172 {1, 2, 3, 4, 'h', 5, 6, 'i'}73 >>>t174 {2, 3, 4}75 >>> s = {555,666}76 >>>
77 >>> s1.isdisjoint(t1) #若s1和t1不存在交集,返回true,否则返回false
78 False79 >>>s1.isdisjoint(s)80 True81 >>>
82
83 >>> s1 = {1,2,3,4}84 >>> t1 = {2,3,5}85 >>> s1.difference_update(t1) #用s1与t1的差集更新替换为s1
86 >>>s187 {1, 4}88
89
90 >>> s1 = {1,2,3,4}91 >>> t1 = {2,3,5}92 >>>s1.symmetric_difference_update(t1) #用s1与t1的对称差集更新替换为s1
93 >>>s194 {1, 4, 5}
2.文件的操作
1.文件的打开模式
2.文件的读取:注意每次打开文件最后都需要 file.close() 进行关闭,也可直接使用【with】语句打开,其可以自动关闭,具体如下所示:
1 f = open("yesterday.txt",'r',encoding="utf-8") #文件的句柄,”r“是以只读模式打开
2
3 print(f.read) #读取yesterday.txt文件内容
4 print(f.readline) #读取一行,yesterday.txt文件内容
5 print(f.readlines) #将文件输出为一个列表,每一行为列表的一个元素
6 f.close()
7
8 #建议的写法,逐行读写,在第十行打印分割线
9 count = 0
10 for i in f:
11 count +=1
12 if count == 10:
13 print("我是分割线".center(40,'-'))
14 print(i)
15
16 for i in range(5): #行数少的时候打印前五行
17 print(f.readline())18
19
20 #逐行读取文件,在第十行的时候输出分割线,占内存较多的写法,不建议
21 for index,i inenumerate(f.readlines()):22 if index == 9:23 print("我是分割线".center(40,"-")+"\n")24 print(i)
25
#读取指定路径的文件
file_path = r'C:\Users\15302\PycharmProjects\GKXXX\day2\yesterday.txt' #注意由于路径中出现 ‘\U’在python中此转义符其后为Unicode编码,故此处路径要前要加 【r】
f = open(file_path,'r',encoding='utf-8')
print(f.read())
#with语句
为避免打开文件后忘记关闭,当with代码块执行完毕时,内部会自动关闭并释放文件资源。with使用方法如下:
with open('file or file_path','r',encoding='utf-8') as file_object:
print(f.read())
with open('file or file_path','r',encoding='utf-8') as file_object1 , open('file or file_path','r',encoding='utf-8') as file_object2#可打开多个
1 with open('111111.txt','rb') as f2:2 for i inf2:3 off = -3
4 whileTrue:5 f2.seek(off,2)6 data =f2.readlines()7 if len(data)>1: #当.readlines() len为2时候,证明读了两行
8 print("last hang",data[-1].decode("utf-8")) #取最后一个值,即最后一行
9 break
10 off *=3 #加大偏移量
读取文件最后一行
2.文件的写入:file.writelines(sequence)的参数是序列,比如列表,它会迭代帮你写入文件。
#w模式,是在文件操作一开始的时候进行判断是否新建和覆盖写入。如果是在文件操作过程中,一直写入也不会覆盖
1 f = open("yesterday.txt",'w',encoding="utf-8") #文件的打开模式为 ‘w’,此时文件可以写入,文件不存在则新建,若存在则清空再写入,切记注意。2
3 f.write("莺歌燕舞太守醉,\n")4 f.write("明朝酒醒春已归。\n")5 f.write('枕上诗书闲处好,\n')6 f.write('门前风景雨来佳。')7
8 f.close()
关于文件光标,默认在0,.read()后在最后
with open('111111.txt','r+') as f2: #txt 文件里为gkxxx,写完后为 2kxxx
print(f2.tell()) #0
f2.write('2')print(f2.tell()) #1
f2.read()print(f2.tell()) #5
View Code
3.文件的方法:
1 f = open("yesterday.txt",'r',encoding="utf-8")2 print(f.readline()) #打印第一行3 print(f.tell()) #打印指针位置
4 print(f.seek(10)) #将指针位置放到第十个字符处,注意虽然可以把指针放到指定处,但是在‘a’,‘r+’模式下,f.write()还是会默认追加到文件末尾,不会在指针处追加5 print(f.tell()) #打印指针位置
>>>>>>>>结果如下所示:
6 Somehow, it seems the love I knew was always the most destructive kind7
8 72
9 10
10 10
with open('111111.txt','rb') as f2: #seek补充用法print(f2.readlines())print(f2.seek(-2,2))>>>[b'2kxxx\r\n', b'dfdf'] #二进制中的换行 \r\n
9 #对于111111.txt中的内容,倒数2个字符,相当于正数9个字符
#seek有三种模式 0,1,2
0:默认用法1:相对位置seek2:倒数seek,当用1,2时候,模式要换成rb,
seek用 seek(int,2) 这种模式读取文件最后一行
1 with open('111111.txt','rb') as f2: #读取最后一行的思路
2 for i inf2:3 off = -3
4 whileTrue:5 f2.seek(off,2)6 data =f2.readlines()7 if len(data)>1:8 print("last hang",data[-1].decode("utf-8"))9 break
10 off *=3
#truncate方法
1 f = open("yesterday.txt",'a',encoding="utf-8")2 f.truncate(10) #截取文件前十个字符,其余内容删除
>>> somehow,i
#flush方法
1 importtime,sys2 for i in range(10):3 sys.stdout.write("#")4 sys.stdout.flush()5 time.sleep(0.5) #每隔0.5秒打印一个“#”,一共打印十个
4.文件的修改
1 f = open('yesterday.txt','r',encoding='utf-8')2 f2 = open('yesterday3.txt','w',encoding='utf-8')3
4 for line inf:5 if "肆意的快乐" inline:6 line = line.replace("有那么多肆意的快乐等我享受","有那么多肆意的快乐等Gkx享受")7 f2.write(line)
>>>>>
将yesterday.txt文件逐行读取,修改其中一行,并write到新文件中
with open('upload_file','r') as f:
line=f.readline()while line != '':print(line)
line= f.readline()
判断文件读取结束