Collection operations
A collection is an unordered, non-repeating combination of data, and its main functions are as follows:
Go heavy, turn a list into a set, and automatically go to the weight
Relationship test, test the intersection of two sets of data, difference set, and set of relationships # Author:zhiyu Su
list_1= [1,4,5,7,3,6,7,9]list_1=Set(list_1) #转换为集合list_2=Set([2,6,0, the, A,8,4,]) Print (list_1,list_2)" "#交集 Intersectionprint (List_1.intersection (list_2)) #并集 Unionprint (List_1.union (list_2)) #差集 difference
Print (List_1.difference (list_2)) print (List_2.difference (list_1)) #子集 list_3=Set([1,3,7]) print (List_3.issubset (list_1)) #判断是否是子集 whether LIST3 is a subset of List1 print (List_1.issuperset (list_3)) #判断是否是父集 List1 is the parent set of the List3 # symmetric differential Set print (List_1.symmetric_difference (list_2)) #把俩个集合里面互相都没有的取出来print ('---------------------') List_4=Set([5,6,7,8]) print (List_3.isdisjoint (list_4)) #判断是否有交集#交集print (list_1&list_2) #并集print (list_2|list_1) #差集print (list_1-list_2) #inchList1 but notinchlist# symmetrical differential set print (List_1^list_2) Print ('------------------') #增list_1. Add (999) list_1.update ([888,777,555])
#使用remove () can delete an item
T.remove (' h ')
#set的长度
Len (s)
Print (List_1.pop ()) #s随机删
X in S tests if X is a member of S X not in s to test if X is not a member of S s.issubset (t) s <= T tests if every element in S is in t S.issuperset (t) s >= T tests if every element in T is in S s.union (t) s | t returns a new set containing each element in S and T s.i Ntersection (t) S & T returns a new set containing the common elements in S and T s.difference (t) s-t Returns a new set containing S but T The element s.symmetric_difference (t) s ^ T returns a new set containing the non-repeating element in S and T s.copy () returns a shallow copy of the set "s"
File operations
- Open the file, get the file handle and assign a value to a variable
- Manipulating a file with a handle
- Close File
Open File
#data = open ('Yesterday', encoding='Utf-8'). Read ()
F= Open ('Yesterday2','a', encoding='Utf-8'#文件句柄: The file's memory object filename character Set the starting position on the hard disk #a=Append additional f.write ('When I am young I listen to the radio\n') #写入data=F.read () #read的读第二遍无法获取内容因为第一次读取之后光标到尾端print ('----Read,', data) f.close () #关闭模式
File open mode
py3.0 transfer only in binary mode (files are binary encoded and not binary)
f = open (' Yesterday2 ', ' R ', encoding= ' utf-8 ') #文件句柄 read
f = open (' Yesterday2 ', ' W ', encoding= ' utf-8 ') #文件句柄 write
f = open (' Yesterday2 ', ' a ', encoding= ' utf-8 ') #文件句柄 append
f = open ('Yesterday2','r+', encoding='Utf-8'reads a file #文件句柄 read and write, and then appends the F= Open ('Yesterday2','w+', encoding='Utf-8'#文件句柄 write to create a file and write F= Open ('Yesterday2','w+', encoding='Utf-8') #文件句柄 append write F= Open ('Yesterday2','WB') #文件句柄 binary read f.write ('Hello binary\n'. Encode ()) #二进制写入f. Close ()
Read file
print (F.readline ()) #readline读取一行for i in range (5): #读取五行
Print (F.readline ())
Print (F.readlines ()) #文件里所有内容一列表形式打印出来
For line in F.readlines (): #循环打印文件
Print (Line.strip ()) #打印时会出现空行 the line break for each row when reading a file
#循环不打印文件第10行
For Index,line in Enumerate (F.readlines ()): #文件全部获取到内存里 suitable for small file reads
If index== 9:
Print ('----------')
Continue
Print (Line.strip ())
#高效版及时读写 iterators
Count = 0
For line in F:
Count + = 1
if Count = = 9:
Print ('----------')
Continue
Print (line)
File cursor operations and other actions
F.seek (f.truncate)#如果不写值就是清空, the write value is truncated print (F.tell ()) #文件句柄指针print ( F.readline ()) print (F.readline ()) print (F.readline ()) print (F.tell ()) #读取指针, by Byte F.seek (0 ) #移动光标print (F.readline ()) print (f.encoding) #打印文件的编码print (F.flush ()) #实时更新到硬盘里
Print (F.fileno ()) #返回文件句柄的编号 the currently open IO interface
Progress bar for flush real-time Refresh
# Author:zhiyu Suimport sys,time for in range: sys.stdout.write ('/') Sys.stdout.flush () time.sleep (0.1)
With statement
To avoid forgetting to close after opening a file, you can open multiple files at the same time after py2.7 through the admin context with
With open ('log','r') as f:print (F.readline ())
With open (' Log1 ') as Obj1,open (' log2 ') as Obj2:
.....
Character encoding and transcoding
1. In Python2 the default encoding is ASCII, the default is Unicode in Python3
2.unicode is divided into utf-32 (4 bytes), utf-16 (two bytes), Utf-8 (1-4 bytes), so utf-16 is now the most commonly used Unicode version, but in the file is still utf-8, Because UTF8 province space Udf-8 English with a byte Chinese three bytes
3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string
Function
What is a function
Definition: A function that encapsulates a set of statements by a name (function name), and to execute the function, simply call the name of its functions
Characteristics:
- Reduce duplicate code
- To make the program extensible
- Make programs easier to maintain
Learn Python Basics--------3