Higher-order functions will not use? Teach you the most useful python three higher-order functions

I believe that many readers still point to open a look ignorant, what is the higher-order functions, high in there?

Big cat would let us talk about what is the higher-order functions, as long as the function is defined to meet the following two points, can be called higher-order functions.


1. accepts as input one or more functions

2. The output of a function


It is not more ignorant, in fact, the above definition of adult word is translated as a parameter of the function return value of the function or function as a function, which is called higher-order functions.
The most common higher-order function is a function closure decorator is syntactic sugar to achieve closure, python decorator is one of the large number of technical solutions used only shows the importance of higher-order functions.
Decorator is a higher-order function output as a function return value, we mainly speak today to accept one or more parameters as input of the higher-order functions, the main map, reduce and filter functions.
map function

 

 


map will do the mapping for the specified sequence according to the functions provided. The first parameter is a function, the latter is an iterative parameter object. A parameter sequence for each element for which function the function, the return value is the new list (python2) or iterables (python3). map (function, iterable, ...)
Case: List (Map (lambdax: X ** 2, [. 1, 2,. 3,. 4,. 5])) the Result: [. 1,. 4,. 9, 16, 25] List (map (lambdax, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])) Result: [3, 7, 11, 15, 19] list (Map (STR, [. 1, 2,. 3,. 4,. 5])) the Result: [ '. 1', '2', '. 3', '. 4', '. 5']
the reduce function

 

 


reduce function elements in a sequence parameter will be accumulated. reduce an iterative function of all data objects in the following: (two parameters) of the first set of first and second elements with the function operates in the function passed to reduce, re results obtained with the third data function with a function operation to obtain a final result.
Python2 is a built-in function in reduce, python3 there needs to be imported by functools library. from functools import reduce
Syntax: the reduce (function, Iterable)
Case: the reduce (lambdax, Y: X + Y, [1,2,3,4,5]) the Result: 15reduce (the lambda X, Y: X * Y, Range (1, 6)) Result: 120

filter function

 

 


filter函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表(python2)或迭代器(python3)。该函数接收两个参数,第一个为函数,第二个为可迭代对象,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表(或迭代器)中。
语法:filter(function, iterable)
案例:List(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))Result: [2, 4, 6, 8, 10]list( filter((lambda x: x < 0), range(-5,5)))Result: [-5, -4, -3, -2, -1]a = [1,2,3,5,7,9]b = [2,3,5,6,7,8]list(filter(lambda x: x in a, b))Result: [2, 3, 5, 7]

作  者:Testfan 大猫
出  处:微信公众号:自动化软件测试平台
版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出文章链接

 

Guess you like

Origin www.cnblogs.com/testfan2019/p/12400968.html