One, iterator 1. Iterator definition
An iteration is a repeating process that repeats one iteration at a time, and the result of each iteration is the initial value of the next iteration.
L = ["AAA", "BBB", "CCC"]count = 0while count< len (l): #每次重复完成后count都是下一次的初始值 print(l[count ]) count+=1
Why iterators are required: for sequence types str, list, tuple can rely on index iteration value, for Dict, set, file need to provide a way to not rely on index value .
An iterative object: An object with a built-in __iter__ method. strings, lists, tuples, dictionaries, collections, and files are objects that can be iterated.
Iterator object: An iterator object that results from an iterative object that executes the obj.__iter__ () method. The iterator object has built-in __iter__ and __next__ methods. The file object is an iterator object.
Summary :
1. An iterator object is not necessarily an iterator object.
2. The iterator object must be an iterative object.
3. Call the Obj.__iter__ () method to get the iterator object, and if it is an iterator object, the execution of the method will still be itself.
2. Use of iterators
After an iterative object calls the Obj.__iter__ () method to get an iterator object, the iterator object can use OBJ.__NEXT__ () to iterate over the value, which can be called directly from the obj.__next__ ().
OBJ.__NEXT__ () is equivalent to next (obj), and calling __next__ when the value is taken will throw a stopiteration exception.
#每次调用__next__方法取一次值, after fetching the method will throw stopiteration exception L = ["AAA", "BBB", "CCC"]iter_list = l.__iter__() print (Iter_ list.__next__()) print (iter_list.__next__ ()) print ( iter_list.__next__()) Print (Iter_list.__next_ _())
Catching exceptions using Try...except
D = {"A": 212, "B": 111, "C": 222}iter_dict = d.__iter__() while True: try: print (Next (iter_ dict)) #字典迭代取到的为key except stopiteration: break
For loop principle
For k in obj
A) the obj__iter=obj.__iter__ () after calling in is given an iterator object.
b) Execute k=obj_iter.__next__ () assigns the value to K, and then executes the loop body.
c) Repeat the process 2 until the stopinteration exception is caught, ending the loop.
3. Advantages and disadvantages of iterators
Advantages:
A) provides a unified, non-dependent index method of value, which provides a basis for the implementation of the For loop.
b) iterators have only one value in memory at the same time, saving more memory.
Disadvantages:
A) can only be taken back to the value, for a one-time.
b) Before next executes, it is not possible to count the counts, without obtaining the length.
Ii. generator 1. Generator definition
As long as there is a yield keyword inside the function, the generator does not execute the func (), and the generator is an iterator.
Def func (): print ("1111111111") yield 1 print ("2222222222") yield 2 print (" 3333333333 ") yield 3g = func () #g为生成器, the generator is the iterator print (Next (g)) #func () begins execution to the first yield, and print yield return value print (Next (g)) #func () executes to the second yield, and prints yield return value print (Next (g) )
Yield Features:
A) yield provides a way to customize iterators
b) The difference between yield and return: Yield can return multiple values, and return returns only once. The function pauses with a continuation state with yield saved.
Analog piping for tail-f Access.log | grep "404"
1 #tail-f Access.log|grep "404" 2 Import time 3 def Tail (filepath): 4 with open (filepath, ' RB ') as F:5
f.seek (0,2) 6 while true:7 line = f.readline () 8 if line:9 yield line10 else: one Tim E.sleep (0.05) def grep (lines,pattern): + For lines in lines: #调用tail后得到生成器对象, you can use the for to iterate through each of them line =line . Decode (' UTF-8 ') if pattern in line:17 yield line18 res = grep (Tail ("Access.log"), "404") #res也为生成器对象, You can use the for to iterate over the value of line in res:21 print
View Code2. The co-process function
Yield in the form of an expression, when used, must wear none,g.send (None) for the first time equal to Next (g)
1 def Eater (name): 2 print ("%s started eating"%name) 3 food_list = [] 4 while true:5 food = yield
food_list 6 print ("%s ate%s"%
food_list.append (food) 8 9 G = Eater ("xxx") #创建生成器g10 res1 = G.send (None) #初始化yiel D, get the empty list returned print(res1) res2 = g.send ("Rice") #发送 "rice" to yield, continue executing the code to the next yield, return the list to res213 print(res1) 14 G.close () #结束迭代15 res3 = g.send ("noodles") #无法发送和获取值16 print (RES3)
View Code
Implementation features: Grep-rl ' python '/etc
1 #从某个路径下搜索所有包含某个字符串的文件的路径 2 ImportOS 3 4 defInit (func): 5 def inner (*args,**Kwargs): 6 res = func (*args,**Kwargs) 7Next (RES) 8 returnRes 9 returnInner10 #列出某个路径下所有文件的绝对路径12@init13 defList_path (target):True:15 File_path = yield16 g =Os.walk (File_path) Pardir,_,files inG:18 for file infiles:19 Abs_path = "%s\%s"%(Pardir,file) 20Target.send (Abs_path) #打开某个文件23@init24 def Openner (target): true:26 Abs_path = yield27 with open (Abs_path, "RB" ) as f:28 target.send ((abs_path,f)) #读取文件中每一行31 @init32 def read_line (target): while true:34 abs_path,f = yield35 for Line in f:36 flag = target.send ((abs_path,line)) PNs if flag:38 break39 #从一行中查找某个字符41 @init42 de F find_str (Target,pattern): flag = False #用于判断某个文件是否包含有重复的某个字符44 pattern = Pattern.encode ("Utf-8" ) 45 While true:46 abs_path,line = yield flag47 if pattern in line:48 target.send (Abs_path) by flag = True50 #将结果打印出来52 @init53 def print_path (): Si while true:55 abs_path = yield56 print (abs_p ATH)/List_path (Openner (Read_line (Find_str (Print_path (), "abcdef" ))] (r "G.send")
View Code
3.yield Summary
A) functions can be made into iterators
b) compared to return, you can return multiple values to suspend/Save the function's running state
Third, process-oriented programming
Process-oriented is a kind of thinking and thinking, not relying on specific language or grammar, the core idea is the process of two words, that is, according to the pipeline to solve the problem.
The advantage is the complexity of the problem of the process, decomposition into a simple small function.
The disadvantage is that the scalability is poor, modify the pipeline any stage, will affect the other stages.
Suitable for scenarios with low extensibility requirements, such as Linux kernel, git, httpd, etc.
Four or three-tuple expression, list deduction, generator expression 1. Ternary expressions
Name = input (">>:"). Strip () res = "SB" if name== "xxx" Else "NB" #满足if条件, then returns the value before if the value of if is not satisfied then return to else after print (res)
2. List Deduction formula
L = [Str (i) + "xxx" for I in Range (1,10)] #为1-10 All numbers add suffix print(l) L = [Str (i) + "xxx" for I in range (1,20) if i%2==0]
#1-20 all even plus suffix print (l)
3. Generator expression
Changing the list derivation [] to () is the generator expression.
#生成老母鸡, call next when you need to use egg >>> chicken = ("eggs%s"%i for I in range (1,10)) >>> Chicken<generator Object <genexpr> at 0x000001afd64cdf68>>>> next (chicken) ' Egg 1 ' >>> list (chicken) # The first egg has been down, chicken can be iterated, so can be converted to list [' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']
4. Declarative programming
A) capitalize the letters in l=[' xxx ', ' abc_sb ', ' aaa ', ' BBB ']
l=[' xxx ', ' abc_sb ', ' aaa ', ' bbb ']l = [I.upper () for I in l]print (l)
b) filter out the name l=[' xxx ', ' abc_sb ', ' aaa ', ' BBB ', and save the remaining name length
l=[' xxx ', ' abc_sb ', ' aaa ', ' bbb ']l = [Len (i) for I in L if not I.endswith ("SB")]print (l)
c) The length of the longest line in the file Test.txt (length by character count, need to use the Max function)
With open ("Test.txt", ' R ', encoding= "Utf-8") as f: res = max (len (line) to line in f) print (res)
D) Find the total number of characters in the file Test.txt? Think about why the sum of n times after the first time is 0? (need to use the SUM function)
With open ("Test.txt", ' R ', encoding= "Utf-8") as f: res = SUM (len (line) to line in f) print (res)
The generator can only take the value back, once the value has been taken, and then the generator will be summed to a result of 0
V. Recursion and dichotomy 1. Recursive invocation definition
In the process of invoking a function, the function itself is called directly or indirectly, called a recursive call.
Recursive calls are divided into two stages: recursion and backtracking.
Python uses sys.gettrcursionlimit () to view the number of layers that can be called recursively.
Import Sysprint (Sys.getrecursionlimit ()) #默认支持1000层递归调用l=[1,[2,[3,[4,[5,[6,[7,]]]]]]]def Func (L): for item in L: if Type (item) is List:func (item) Else: Print(item) func (l) /c17>
Recursive invocation features in 2.python
Recursive calls in Python are inefficient and require the current state to be retained when entering the next recursion. There are tail recursion optimizations in other languages, not in Python, and the hierarchy is limited.
Requirements for recursive use:
A) must have a definite end condition
b) The problem size is less than the last time you enter a deeper level of recursion
c) Recursive efficiency is not high, too many layers easily lead to stack overflow
3. Two-point method
1 L = [2,3,5,6,8,12,15,16,19,22,35,45,56,62,98,122,321] 2 def Binary_search (l,num): 3 print(l) 4 Mid_index = Len (l)//2 5 If Len (l)! = 0:6 if num < L[mid_index]: 7 binary_search (l[0:mid_index-1
],num) 8 elif num >
l[mid_index]: 9 Binary_search (l[mid_index+1:],num)-Else: print ("Find it" c14>)-Else: print ("Can ' t find%s"% num) binary_search (l,321)
two-part methodVi. Anonymous functions
Anonymous functions are used once and are defined on demand at any time, and can be applied to functions such as Max, Min, sorted, map, reduce, filter, and so on.
#找出薪水最高的人salaries = { ' abc ': 3222, ' def ': 111, ' AAA ': 431, ' xxx ': 4133} #普通方法g = Zip (Salaries.values (), Salaries.keys ()) #g为迭代器print (Max (g)) #max按照每次取值的第一个数来比较 # method to define the function, print out the name of the person with the highest salary def func (k): Return salaries[k]print (max (Salaries,key=func)) #排序是按照key来排序, showing results shown by default results of salaries # using the anonymous function print ( Max (Salaries,key=lambda k:salaries[k]))
Seven, built-in functions
Sorted
#薪水排序, output name salaries = { ' abc ': 3222, ' def ': 111, ' AAA ': 431, ' xxx ': 4133 }print (Sorted (Salaries,key=lambda k:salaries[k]) print (sorted (Salaries,key=lambda k:salaries[k],reverse= True))
Map
#给列表中所有元素批量添加后缀 # Common method names = [' xxx ', ' aaa ', ' eee ']l =[]for name in names: res = '%S_SB '%name L.append (res) print(l) #使用map添加g = map (lambda name: '%s_sb '%name,names) print (g) #g为迭代器print (List (g))
Filter
#筛选出某个序列中特定元素names = ["XXX_SB", "AAA_SB", "SSS", "EE_SB"]g = filter (lambda i:i.endswith ("SB"), names) print (g) Print (List (g))
Reduce
#筛选出某个序列中特定元素 # Two consecutive elements in a sequence are processed from Functools import reduceprint (Reduce (lambda x,y:x+y,range (1,101))) #最后的100为初始值, you can not add it.
Python Learning note Four (iterators, generators, built-in functions)