[Python] anonymous functions and higher-order functions

Source: Internet
Author: User

1. Anonymous functions

Lambda: A syntax, three attributes, four usage

Grammar:
Lambda argument_list:expression
Argument_list and expression are custom-defined by the user

(1) Argument_list is a list of parameters. Its structure is the same as the parameter list of functions in Python.

A, ba=1, B=2*args**kwargsa, b=1, *args empty ...

(2) expression is a formula for a parameter. The arguments that appear in the expression need to be defined in argument_list, and the expression can only be single-line, as follows:

1Nonea + b sum (a) 1 if a > Else 0

Lambda Argument_list:expression represents a function, an anonymous function.

Three features:
(1) The lambda function is anonymous; the so-called anonymous function, popularly speaking, is a function without a name. The lambda function has no name;
(2) The lambda function has input and output, the input is the value passed to the parameter list argument_list, and the output is the value calculated from expression expressions;
(3) Lambda function is generally simple, single-line expression determines that the lambda function can not complete complex logic, can only complete a very simple function

Four uses:
(1) Assigning a lambda function to a variable, indirectly invoking the lambda function through this variable
(2) The lambda function is returned to the caller as the return value of the other function
(3) Passing a lambda function as an argument to another function
(4) Assign a lambda function to another function to replace it with the lambda function

Python Programming method
Python programming is divided into:
(1) Process-oriented
(2) Functional programming
Do not use variables to save state, do not modify variables
function is variable
(3) Object-oriented

2. Higher-order functions

(1) function receive parameter is a functional name
(2) Include function in return value
Passing functions as arguments, such functions are called higher-order functions.

(1) Map

The map function receives two parameters, one is a function and the other is iterable. Map functions The incoming function to each element in turn and returns the result as a new iterator

def f (x): return x * XM = map (f, [1, 2, 3, 4]) # iterator is an inert sequence, so the list () function allows it to calculate the entire sequence and return a listprint (list (m)) # Line result: # [1, 4, 9, 16]

(2) Filter
Filter receives a function and a sequence. Filter () applies the incoming function to each element sequentially, and then decides whether to persist or discard the element based on whether the return value is True and False.

def is_odd (n): return n% 2 = = # F = Filter (is_odd, [1,2,3,4,5,6,7]) # equivalent to F = filter (lambda n: = = 2 = = 1, 4,5,6,7]) Print (list (f)) # Execution Result: # [1, 3, 5, 7]

(3) Reduce
Reduce takes a function on a sequence, and the function must receive two parameters, and reduce calculates the result and the next element cumulatively.

From Functools import reducedef foo (x, y): return x + y# r = Reduce (foo, [1,2,3,4]) # equivalent to: R = Reduce (lambda x, y:x + y, [1,2,3,4]) print (R)

[Python] anonymous functions and higher-order 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.