18. Collection
Unordered, non-repeating data combination
1. Go to the heavy
2. Relationship Testing
List_1 = [1, 4, 5, 7, 3, 6, 7, 9]
List_1 = Set (list_1)
List_2 = Set ([2, 66, 0, 6, 22, 8, 4])
List_3 = Set ([1, 5, 7])
# intersection
# set ([4, 6])
Print List_1.intersection (list_2)
# and set
# set ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 66, 22])
Print List_1.union (list_2)
# difference Set
# set ([1, 3, 9, 5, 7]) 1 miles without
Print List_1.difference (list_2)
# subset-Determine if List1 is a subset of 2
# False
Print List_1.issubset (list_2)
# True
Print List_3.issubset (list_1)
# Parent Set-Determine if List1 is 2 parent set
# False
Print List_1.issuperset (list_2)
# True
Print List_1.issuperset (list_3)
# symmetric difference sets, complete set-intersection
# set ([0, 1, 2, 3, 5, 7, 8, 9, 66, 22])
Print List_1.symmetric_difference (list_2)
# Determine if there is an intersection, no return true
# True
Print List_2.isdisjoint (list_3)
# Intersection Characters &
Print List_1 & list_2
# and the Set of characters |
Print List_1 | List_2
# difference-1 has 2 no
Print List_1-list_2
# symmetric difference Set
Print List_1 ^ list_2
# Add an item
List_1.add (999)
# Add multiple items
List_1.update ([888, 777, 555])
# Test if X is a member of a
X in a
# Delete
Pop () # randomly pops a value
# Remove value does not exist when an error occurs
List_3.remove (7)
Print List_3
# Discard deleted value does not exist no error
List_3.discard (6)
Print List_3
19. File operation
f = Open ("x", "R", encoding= "Utf-8") # is opened by default in system encoding
data = F.read ()
Print data
R Read W write a append (not readable)
"+" means you can read and write a file at the same time
r+, read and write the original file append
w+, write a file first and then read and write
A +, Additional Reading
"B" means processing binary files
RB binary file read, non-pass encoding format
WB binary File Write
AB binary file Append
F.readlines () #只适合读小文件 Low loop
For Index,line in Enumerate (F.readlines ()):
If index = = 9:
Print "-" *20
Continue
Print Line.strip ()
# save one row, read a row, delete a row, save only one row in memory, high efficiency, the file is an iterator
Count = 0
For line in F:
Count + = 1
if Count = = 10:
Print "-" *20
Continue
Print Line
F.read (50) Read 50 character (s)
F.tell () reads the position of the file pointer, in character count
F.seek (0) back to a position, here is the beginning
f.encoding () Output file encoding
F.seekable () Determines whether it can be moved, returns True, returns false
F.readable () is readable
F.writeable () is writable
F.flush () forced refresh, default etc file cache to a certain size and then flush to hard disk
# Output to Screen
Import Sys
Sys.stdout.write ("# # #")
Truncate (Ten) #从开头到10字符的位置, after which the truncation clears
After Python 2.7, with also supports the management of multiple file contexts simultaneously, namely:
With open (' Log1 ') as Obj1, open (' log2 ') as Obj2:
Pass
20, character encoding and transcoding
UTF8 is an extended set of Unicode
Utf-8 turn into GBK
S.decode ("Utf-8"). Encode ("GBK")
Explanation: To convert S to Unicode, declare it before it is utf-8, then turn to GBK, declare the encoding it is now converting
Need to know:
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 (accounting for 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 the UTF8 save space
3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string
Python2
#-*-coding:utf-8-*-
Import Sys
Print (sys.getdefaultencoding ())
msg = "I love Beijing Tian ' an men"
msg_gb2312 = Msg.decode ("Utf-8"). Encode ("gb2312")
GB2312_TO_GBK = Msg_gb2312.decode ("GBK"). Encode ("GBK")
Print (msg)
Print (msg_gb2312)
Print (GB2312_TO_GBK)
Python3
#-*-coding:gb2312-*-
Import Sys
Print (sys.getdefaultencoding ())
msg = "I love Beijing Tian ' an men"
#msg_gb2312 = Msg.decode ("Utf-8"). Encode ("gb2312")
msg_gb2312 = Msg.encode ("gb2312") #默认就是unicode, no more decode, hi Big Ben
Gb2312_to_unicode = Msg_gb2312.decode ("gb2312")
Gb2312_to_utf8 = Msg_gb2312.decode ("gb2312"). Encode ("Utf-8")
Print (msg)
Print (msg_gb2312)
Print (Gb2312_to_unicode)
Print (Gb2312_to_utf8)
21. Programming methods and functions
Programming Methods:
1. Object-oriented-class--class
2. Process-oriented-process--def (parameter with no return value)
3. Functional programming--function--def
Three advantages of the function:
1. Code Reuse
2. Maintain consistency
3. Extensibility
22. Parameters
Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric
Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter.
It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value
def test (x, y):
Print (x)
Print (y)
Test (Y=2,x=1) #关键参数, independent of formal parameter order
Test #位置参数, corresponding to the parameter one by one
Test (3, y=2) #先按照位置参数来, key parameter cannot be written in front of position parameter
def test (x,y=2):
Print (x)
Print (y)
Test (1,3)
Default parameter feature: When calling a function, the default parameter must not be passed
Parameter group:
#*args: Receives n positional parameters, transforms the form of a Narimoto group
def test (*args):
Print (args)
Test (1,2,3,4,5) #可接收多个实参, put multiple arguments into the meta Zanzuri
Test (*[1,2,3,4,5]) # args=tuple ([1,2,3,4,5]), consistent with the above
# **kwargs: Receives n keyword arguments, converted to a dictionary form
def test2 (**kwargs):
Print (Kwargs)
Test2 (name= ' Alex ', age=8)
Test2 (**{' name ': ' Alex ', ' Age ': 8})
def test3 (name, **kwargs):
Print (name)
Print (Kwargs)
Test3 (' Alex ')
Test3 (' Alex ', age=18,sex= ' m ')
def test4 (name, age=18, **kwargs):
Print (name)
Print (age)
Print (Kwargs)
Test4 (' Alex ', sex= ' m ', age=3)
Test4 (' Alex ', 4,sex= ' m ')
The path of Python, day3