1.yield meaning of production and generation, the function with yield in Python is called the Generator (Generator)
The larger the #生成10000个元素的List value of the ' for I in range ', the larger the memory consumed in the run will be 10000 for J in Xrange (10000) #xrange返回的不是List, but a Iterable object Each iteration returns the next value, taking up a very small amount of memory space "Def Readfilebyyield (XPath): With open (XPath, ' RB ') as Ty: For line in Ty.readlines (): yield Line #每执行一次yield, the statement is interrupted, and an iteration value is returned #for the loop automatically calls next () for line in Readfilebyyield (' D:\wangyi.py '): print Line
2.Python ternary operation Syntax format: The result of true if the condition is determined if the conditions else is false
1 if 10>9 else 0def threeelementexpression (A, a): return A-B if a > B. Else ABS (A-a) print threeelementexpress Ion (1,5) print threeelementexpression (10,5)
The 3.LAMBDA expression, which functions as a function sketch, is equivalent to an anonymous function, whereas a function declared with DEF is named, such as:
Lambdatest = Lambda x:x**2for i in range (1,5): print lambdatest (i) List_lambda = Range (1,10) #列表中的每个元素加1print map ( Lambda a:a+1, List_lambda) #返回列表中的偶数print filter (lambda a:a% 2 = = 0, List_lambda)
Python learning Experience (iv) yield and ternary operations and lambda expressions