Day12--python higher order functions and anonymous functions

Source: Internet
Author: User
Tags abs

Higher order functions: a function that passes functions as arguments, for example: Def add (x,y,f):    return F (x) + f (Y) print (Add ( -8,11,abs)) Results: 19 Explanation: 1. Call the Add function to perform ABS (-8) and ABS (11), respectively, to calculate their value 2. Finally, when you do and operate the map () function     map function, a python built-in high-order function, It takes a function f and a list, and passes the elements of the list to the function f, and then returns a function f that finishes the list of all list elements, as follows: Def f2 (x):    return x*yl = [1,2,3,4,5,6] Print (Map (f2,l)) results: [1,4,9,26,25,36] Interpretation: 1.L is a list, the elements of this list into the function F2, the square of each element 2. Combine the final results of all the calculations into a new list, as shown in the new results: The  reduce () function of the reduce () function is also a high-order function built into Python, and the reduce () function receives parameters similar to map (), a function f, a list, but behaves differently from map (), reduce () The incoming function f must receive two parameters, the first call is to pass the list of the first two elements to F, the second call, that is, the first two list elements of the results of the calculation as a parameter, the third element of the list as a second parameter, the introduction of F to operate, and so on, and returns the final result value. The examples are as follows: Def f (A, B)     return a + bprint (reduce (f,[1,2,3,4,5],10) #f后面是列表结果: 25 Explanation: 1. Calculates a = f (for a) value of 32. Calculate B = f (a,3) A value of 63. Calculates a value of C = f (b,4) of 104. Calculates a value of D = f (c,5) of 155. Calculating the value of F = d (d,10) is 25, which is the equivalent of 1+2+3+4+5+10, the last 10 is a default value, and is finally executed again.  filter () function     Filter The English word is the meaning of filtering, the filter () function is another useful high-order function built into Python, the filter () function receives a function f and a list, The function f is to judge each element, return True or False,filter () according to the judgment knotAutomatically filters out elements that do not meet the criteria and returns a new list of eligible elements. Examples are as follows: Def is_odd (x):    return x% 2 = = 1print (Filter (Is_odd,[1, 2, 3, 4, 5, 6, 7)) Result: [1,3,5,7] Explanation: The element of 1.list is passed to the The function is_odd2.is_odd determines whether each element meets the condition, leaves the qualifying leave, and does not meet the conditions of the 3. Make a new list     #高阶函数map () function, and the first function is a custom function , the second parameter is an iterative object l = [1, 2, 3, 4, 5] #把此处 [] instead of () The final result or a list, the return value of map is a list def f2 (x):    Return X*XM = Map (f2,l) print (Ty PE (m)) print (' # ' *50) print (m)   #reduce () function # The incoming function must receive two parameters, and pass the two arguments of the iterated object as the arguments of the function, passing in the F function # The result of each F operation as the first argument, You can iterate over the next element of an object as another argument, passing in the function F # with some analogy, and finally get the result print (' # ' *50) def f (A, B):    return a + bprint (reduce (f,[1, 2, 3, 4, 5], 10 ) #f后面是列表,  print (' # ' *50) #filter () function # each time an element of an iterative object is passed in, and if it returns True, the element is preserved, and if returned to False, the element is not preserved c = [1, 2, 3, 4, 5, 6, 7, 8 ]def is_odd (x):    return x 2 = = 1print (filter (IS_ODD,C)) print result is: 

anonymous function: A function without a name, why is it useful to set up an anonymous function? A lambda function is a minimal function that quickly defines a single line, and can be used wherever a function is required. General version: Def fun (x, y) retrun X*ylambda version: R = Lambda X,y:x*yprint (r (2,3)) print (Type (r ) Results: 6 #匿名函数, not too much, Def sum (x, y): return x+ym = Lambda x,y:x+yprint (m) print (' # ' *20) print (M (4,5)) #sorted () is Gao Jiahan number # Sort the dictionary mm = dict (A=1, c=3, b=10, d=9) print (' # ' *20) print (mm) for I in Mm:print iprint (' # ' *) for J in Mm.iteritems ():p rint (j) Print (' # ' *20) test = sorted (Mm.iteritems (), Key=lambda d:d[1]) prints (test) results are:

Day12--python higher order functions and anonymous functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.