Python learning summary, python Summary
Iterator: def gen (): a = 100 yield a = a * 8 yield a yield 1000for I in gen (): print (I) to create a function, loop body, yield returns a value after the loop. Call the function to print the cyclic result: 1008001000 table derivation: L = [x ** 2 for x in range (10)] print (L) is equivalent: M = [] for x in range (10): M. append (x ** 2) print (M): [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] xl = [1, 3, 5] yl = [9, 12, 13] L = [x ** 2 for (x, y) in zip (xl, yl) if y> 10] print (L) is equivalent to for (x, y) in zip (xl, yl): if y> 10: print (x) we can print out the value of X corresponding to zip with the Y value greater than 10 to print the value of X:> 3> 5 to print the value of L: [9, 25]
# Lambda function def test (f, a, B): print ('test') print (f (a, B) test (lambda x, y: x ** 2 + y), 6, 9) # Use the lambda anonymous function to pass values to the f parameter, which can be in different forms. Print the result:> test (f (a, B) is equivalent to a = 6 passed to x, B = 9 passed to y, f value> 45 # map () re = map (lambda x: x + 3), [1, 3, 5, 7]) print (list (re )) # A parameter x in map that passes the values in the following list to x at a time, which is equivalent to adding 3 to the values in the list in sequence. The values are in the form of a list. Print results: [4, 6, 8, 10] re2 = map (lambda m, n: m ** n), [,], [,]) print (list (re2): [1, 64,218 7, 65536] # filter () def abc (a): if a> 100: return True else: return Falsenewlist = filter (abc, [101,200,]) print (list (newlist) # create a function. abc has a parameter. The filter function in newlist transmits the value of the List to parameter a in function abc once. The value is in the form of a list. Print results: [101,200] # reduce () # from functools import reduce # Because python does not support the reduce function, you can import a single reduce function in the functools library import functools # You can directly import the entire library print (functools. reduce (lambda x, y: x + y, range (1,101) # The reduce function is the first time to pass the values in the list to the sum of the two parameters and 3, add a parameter 3. Equivalent to the sum of 1. Printed result:> 5050