The content of this section
1. Collection Operations
2. File operation
3. Character encoding and transcoding
4. Function operation
1. Collection Operations
A set is an unordered, non-repeating combination of data;
1.1 Common operations
Its role is to:
1) Auto-weight: List becomes set, auto-weight;
2) Relationship testing: Test the relationship between two sets of data, intersection, set, difference, symmetric difference set, parent set, subset, etc.
#交集 intersection &>>> list_1 = set ([1,4,4,5,6,7,9,10]) >>> list_2 = set ([2,45,6,11]) >> > Print (list_1.intersection (list_2)) #list_1和list_2的交集 {6}>>> print (list_1 & list_2) {6} #并集 Union |> >> list_1 = set ([1,4,4,5,6,7,9,10]) >>> list_2 = set ([2,45,6,11]) >>> print (List_1.union (list_2 ) {1, 2, 4, 5, 6, 7, 9, ten, one, 45}>>> print (list_1 | list_2) {1, 2, 4, 5, 6, 7, 9, ten, One,} #差集 in List_1 but n OT in list_2----difference>>> list_1 = set ([1,4,4,5,6,7,9,10]) >>> list_2 = set ([2,45,6,11]) >>& Gt Print (List_1.difference (list_2)) {1, 4, 5, 7, 9, 10}>>> print (list_1-list_2) {1, 4, 5, 7, 9, ten} #对称差集 symmetric_d Ifference----Remove the same data after merging >>> list_1 = set ([1,4,4,5,6,7,9,10]) >>> list_2 = set ([2,45,6,11]) >> > Print (list_1.symmetric_difference (list_2)) {1, 2, 4, 5, 7, 9, ten, one, 45}>>> print (list_1 ^ list_2) {1, 2, 4, 5, 7, 9, ten, one, one} #子集 issubset≫>> list_1 = set ([1,2,3,4,5,6]) >>> list_2 = set ([1,4]) >>> print (List_1.issubset (list_2)) false>>> Print (List_2.issubset (list_1)) true# parent set issuperset>>> list_1 = set ([1,2,3,4,5,6]) >> > list_2 = Set ([1,4]) >>> print (List_1.issuperset (list_2)) true>>> print (List_2.issuperset (list_ 1)) false# No intersection is true, there is an intersection of false>>> list_1 = set ([1,2,3,4,5,6]) >>> list_2 = set ([1,4]) >>> Print (List_1.isdisjoint (list_2)) false
1.2 Basic Operations
#添加一项 >>> List1 = set ([1,3,4,6,7]) >>> List1.add (Ten) >>> print (List1) {1, 3, 4, 6, 7, ten} #添加多项 >>> List1 = set ([up]) >>> list1.update ([6,8,10]) {8, 1, 2, 10, 6}
#remove---Delete an element that does not exist error >>> List1 = set ([1,2,6,8,10]) >>> list1.remove (2) >>> print (List1) {8, 1, 6}>>> list1.remove (one) >>> print (List1) Traceback (most recent call last): File "c:/users/ administrator/pycharmprojects/cc/day3/set_test.py ", line 2, in <module> list1.remove (one) keyerror:11# Discard---The friendly version of remove, removing non-existent elements does not error >>> List1 = set ([1,2,6,8,10]) >>> List1.discard (2) >>> Print (List1) {8, 1, 6}>>> List1.discard (one) >>> print (List1) {8, 1, 2, ten, 6} #pop---randomly deleted >>> List1 = Set ([1,2,6,8,10]) >>> list1.pop () >>> print (List1) {1, 2, 6} ---Because the collection is unordered, it randomly deletes an item
2. File Operation
Procedure for file operation:
- Open the file, get the file handle and assign a value to a variable
- Manipulating a file with a handle
- Close File
2.1 Open File
#文件句柄 = open (' File path ', ' mode ')
File name: Yesterday
Somehow, it seems the love I knew is always the most destructive kind I don't know why, I went through the most devastating of the kind yesterday when I am young yesterday when I Young and frivolous The taste's life is sweet as rain upon my tongue like the tip of the tongue I teased at live as if it were a foolish game I tease life See it For the stupid game the evening breeze is like the night breeze may tease the candle flame tease Candle Flames The Thousand Dreams I dreamed
The mode of opening the file is:
- r, read-only mode "default"
>>> f = open ("Yesterday, ' r ', encoding= "Utf-8") >>> a = F.read () >>> print (a) >>> f.close () Somehow, it seems the love I knew W As always the most destructive kind somehow I went through the most devastating of the kind of love yesterday when I'm young yesterday when I was a teenager the taste of life is sweet living aids Taste is sweet as rain upon my tongue just like the tip of my tongue I teased at life as if it were a foolish game I tease the lives as stupid games the means the evening breeze just like the night The evening breeze may tease the candle flame the Flame of the Candle, the Thousand Dreams I dreamed
- W, write-only mode "unreadable; not exist" created; empty content;
>>> f = open ("1.txt", ' W ', encoding= "Utf-8")
- A, append mode "readable; not present, create; append content only;"
>>> f = open ("1.txt", ' a ', encoding= "Utf-8")
"+" means you can read and write a file at the same time
- r+, read and write "readable, writable"
>>> f = open ("1.txt", ' r+ ', encoding= "Utf-8")
- w+, write "readable, writable"
>>> f = open ("1.txt", ' w+ ', encoding= "Utf-8")
- A +, write "readable, writable"
>>> f = open ("1.txt", ' A + ', encoding= "Utf-8")
"B" means to operate in bytes
- RB or R+b
- WB or W+b
- XB or W+b
- AB or A+b
Note: When opened in B, the content read is byte type, and the byte type is also required for writing
2.2 Operating files
Read () reads the entire file every time, and it is typically used to place the contents of a file into a string variable. However, read () generates the most direct string representation of the file content, but it is not necessary for continuous row-oriented processing and is not possible if the file is larger than the available memory.
>>> f = open ("Yesterday, ' R ', encoding=" Utf-8 ") >>> a = F.read () >>> print (a) >>> F.close () Somehow, it seems the love I knew is always the most destructive kind I don't know why, I've been through the most devastating of all the kind yesterday when I am Young yesterday, when I was a teenager, the taste of life is sweet as rain upon my tongue like the tip of the tongue I teased at live as if it were a foolish gam e I tease life to see it as a stupid game the evening breeze is like the night breeze may tease the candle flame tease Candle Flames The Thousand Dreams I dreamed
Once the entire file is read, like Read (), ReadLines () automatically parses the contents of the file into a list of rows that can be used by Python for ... in ... Structure for processing.
>>> f = open ("Yesterday", "R", encoding= "Utf-8") #文件句柄 >>> for Index,line in Enumerate (F.readlines ()): >>> If index = = 9:>>> print ('---I am split line---') >>> continue>>> Print (Line.strip ()) >>> f.close () Somehow, it seems the love I knew is always the most destructive kind I don't know why I went through Always the most devastating of the kind yesterday when I am young yesterday when I was a teenager the taste of life is sweet as rain upon my tongue as rain on the tongue I teased at Life as if it were a foolish game---I'm the split line---the The evening breeze just like the night breeze may tease the candle flame tease the flames of the candle the Thousand Dreams I Dreamed
ReadLine () reads only one row at a time
>>> f = open ("Yesterday", "R", encoding= "Utf-8") #文件句柄 >>> a = F.readline () >>> print (a) > >> F.close () Somehow, it seems the love I knew is always the most destructive kind
- Efficient Read File method
>>> f = open ("Yesterday", "R", encoding= "Utf-8") #文件句柄 >>> for line in f:>>> print ( Line.strip ()) >>> f.close () Somehow, it seems the love I knew is always the most destructive kind somehow I went through the most destructive Sex of that kind of yesterday when I am young yesterday when I was a teenager the taste of life is sweet the taste of living is the sweetness as rain upon my tongue like the tip of the tongue I teased at life a s if it were a foolish game I tease life to see it as stupid games the evening breeze is like the night breeze may tease the candle flame tease candle flame The Thousand Dr Eams I Dreamed
2.3 Management Context--with statements
Above we can see, each open after the need to close the file, very cumbersome ...
To avoid forgetting to close a file after opening it, you can manage the context by:
>>> with open ("Yesterday", "R", encoding= "Utf-8") as F:>>> .....
2.4 Other Extensions
#告知当前光标的位置 >>> f = open (' Yesterday ', ' R ', encoding= ' utf-8 ') >>> print (F.tell ()) 0# cursor back to first >>> F.seek (0) #判断是否可移动 >>> f.seekable# Determine if a file is readable >>> f.readable () #判断文件是否可写 >>> f.writable () # File descriptor F.flieno ()
3. Character encoding and transcoding
4. Function Operation
Python Learning notes-(vii) Python basics-Collections, file Operations & functions