The set operation and its corresponding operator means
that there is no insertion in the set, only addition, because after all, it is unordered
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Vergil Zhang list_1 = [1, 4, 5, 7, 3, 6, 7, 9] list_1 = set(list_1) print(list_1, type(list_1)) list_2 = set([2, 6, 0, 66, 22, 8]) print(list_1,list_2) #Intersection print ( list_1.intersection (list_2)) print (list_1 & list_2 ) #Union print ( list_1.union(list_2)) print (list_1 | list_2) #Difference , list_1 has and list_2 does not have print (list_1.difference (list_2)) print (list_1 - list_2) list_3 = set([1,3,7 ]) print (list_3.issubset(list_1)) #Judging whether it is a subset print (list_1.issuperset(list_3)) #Judging whether it is a parent set #Reverse difference set, take out two mutually exclusive print (list_1.symmetric_difference(list_2)) print (list_1 ^ list_2) list_4 = set([5,6,8 ]) print (list_3.isdisjoint(list_4)) #return true if the two sets do not intersect #Add list_1.add( ' x ' ) to the collection list_1.update(['a', 'v', 'b']) print(list_1) #Delete list_1.remove ( ' a ' ) #Deleting non-existent elements will report an error list_1.discard ( ' v ' ) #Deleting non-existing elements will not report an error print (len(list_1)) # length of list_1 print ( ' a ' in list_1) # judge whether 'a' is in list_1 #Judging whether each element in x is in list_1 x = {1, 3 } print (x.issubset(list_1)) print (x <= list_1) #Judging whether each element in x is in list_1 print (list_1.issuperset(x)) print (list_1 >= x)
·File operation
File operation steps:
1. Open the file, get the file handle and assign it to a variable
2. Operate the file through the handle
3. Close the file
File operation mode:
w: write only
r: read only
a: append
r+: read Write, read first, then write at the end of the file
w+: write read, create a file first, write, then read (useless)
a+: append read and write rb: binary file read, network transmissions are automatically closed
with binary files with open() as file object name:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Vergil Zhang #data = open("yesterday.txt", encoding='utf-8').read() #print(data) #Get the file handle file = open( " PLANET.txt " , ' r ' , encoding= ' utf-8 ' ) # print(file.write("I love Beijing Tiananmen")) #Just append content, doing other operations It will report an error # print(file.read()) #Read file for line in file.readlines(): print (line.strip()) # low loop method for index,line in enumerate(file.readlines()): if index == 9 : print ( ' --- --------This line does not print lyrics ----------------- ' ) continue print (line.strip()) #推荐 count = 0 for line in file: if count == 9: print(line) count += 1 continue print(line) count += 1 print (file.tell()) #Print out the position of the handle, based on the number of characters print (file.readline()) print (file.tell()) file.seek(0) #return to the first character print (file.encoding) #The encoding of the file print (file.fileno()) #File number , operating system maintenance, no need to pay attention print (file.isatty()) #Whether it is a terminal device print (file.seekable()) #Whether the cursor can be moved print (file.flush()) #The contents of the cache are flushed to the hard disk #Force flash to hard disk #Simulation progress bar import sys import time for i in range(20 ): sys.stdout.write("#") sys.stdout.flush() time.sleep(0.1) file.close() ''' file = open("PLANET.txt", 'a', encoding='utf-8') file.truncate(10) file.close() file = open("PLANET.txt", 'w+', encoding='utf-8') file.write("lalallalallala........") file.close() file = open("PLANET.txt", 'rb') print(file.readline()) file.close() ''' file_old = open("PLANET.txt", 'r', encoding="utf-8") file_new = open( " PLANET_after.txt " , ' w ' , encoding= " utf-8 " ) for line in file_old: if " I am your planet around you " in line: line = line.replace( " I am your planet " , " Vergil is Ann's planet " ) file_new.write(line) file_old.close() file_new.close() #使用自动关闭 with open("PLANET.txt", 'r', encoding="utf-8") as file: for line in file: print(line.strip())