函数式编程如今逐渐被广大开发群体接受,愈来愈多的开发者们开始使用这种优雅的开发模式,而咱们使用函数式编程最主要的是须要清楚:java
什么是高阶函数(Higher-order Functions)?python
Python 中高阶函数有哪些?要怎么用?git
在函数式编程中,咱们能够将函数看成变量同样自由使用。一个函数接收另外一个函数做为参数,这种函数称之为高阶函数。github
举个例子:编程
def high_func(f, arr): return [f(x) for x in arr]
上面的例子中, high_func
就是一个高阶函数。其中第一个参数 f
是一个函数,第二个参数 arr
是一个数组,返回的值是数组中的全部的值在通过 f
函数计算后获得的一个列表。例如:数组
from math import factorial
def high_func(f, arr): return [f(x) for x in arr]
def square(n): return n ** 2
# 使用python自带数学函数print(high_func(factorial, list(range(10))))# print out: [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
# 使用自定义函数print(high_func(square, list(range(10))))# print out: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
如同java、scala等语言,咱们不少经常使用的高阶函数基本都一致。在开发中咱们常常使用的最基本的高阶函数其实就几个,而咱们也能够基于这些函数去进行适当的扩展,那么下面开始介绍几种经常使用的高阶函数。数据结构
Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.app
根据提供的函数对指定序列作映射, 并返回映射后的序列,定义:ide
map(func, *iterables) --> map object
function
# 序列中的每一个元素须要执行的操做, 能够是匿名函数函数式编程
*iterables
# 一个或多个序列
正如前面所举的例子 high_func
函数, map
函数是 high_func
函数高阶版,能够传入一个函数和多个序列。
from math import factorial
def square(n): return n ** 2
# 使用python自带数学函数facMap = map(factorial, list(range(10)))print(list(facMap))# print out: [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
# 使用自定义函数squareMap = map(square, list(range(10)))print(list(squareMap))# print out: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
能够看到输出了一样的结果,只是与 python2.X
不用的是, python3.X
中返回 map
类 ,而前者直接返回一个列表。
咱们使用匿名函数,也能够传入多个序列,以下
# 使用匿名函数lamMap = map(lambda x: x * 2, list(range(10)))print(list(lamMap))# print out: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 传入多个序列mutiMap = map(lambda x, y: x+y, list(range(10)), list(range(11, 15)))print(list(mutiMap))# print out: [11, 13, 15, 17]
Apply a function of two arguments cumulatively to the items of a sequence,from left to right, so as to reduce the sequence to a single value.
大体上来说, reduce
函数须要传入一个有两个参数的函数,而后用这个函数从左至右顺序遍历序列并生成结果,定义以下:
reduce(function, sequence[, initial]) -> value
function
# 函数, 序列中的每一个元素须要执行的操做, 能够是匿名函数
sequence
# 须要执行操做的序列
initial
# 可选,初始参数
最后返回函数的计算结果, 和初始参数类型相同
简单举个例子:
# 注意,如今 reduce() 函数已经放入到functools包中。from functools import reduce
result = reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
print(result)# print out 15
咱们能够看到,序列 [1, 2, 3, 4, 5]
经过匿名函数进行了累加。
设定初始值:
# 设定初始参数:s = reduce(lambda x, y: x + y, ['1', '2', '3', '4', '5'], "数字 = ")
print(s)# print out:数字 = 12345
须要注意的是:序列数据类型须要和初始参数一致。
Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
filter()
函数用来过滤序列中不符合条件的值,返回一个迭代器,该迭代器生成那些函数(项)为true的iterable项。若是函数为None,则返回为true的项。定义以下:
filter(function or None, iterable) --> filter object
function or None
# 过滤操做执行的函数
iterable
# 须要过滤的序列
举个例子:
def boy(n): if n % 2 == 0: return True return False
# 自定义函数filterList = filter(boy, list(range(20)))
print(list(filterList))# print out: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# 自定义函数filterList2 = filter(lambda n: n % 2 == 0, list(range(20)))
print(list(filterList2))# print out: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
上面咱们能够看到,列表中不能被 2
整除的数据都被排除了。
Return a new list containing all items from the iterable in ascending order.
A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.
sorted
函数默认将序列升序排列后返回一个新的 list,还能够自定义键函数来进行排序,也能够设置 reverse
参数肯定是升序仍是降序,若是 reverse = True
则为降序。函数定义以下:
def sorted(iterable: Iterable[_T], *, key: Optional[Callable[[_T], Any]] = ..., reverse: bool = ...) -> List[_T]: ...
iterable
# 序列
key
# 能够用来计算的排序函数。
reverse
# 排序规则,reverse = True降序,reverse = False 升序(默认)。
举个简单例子:
list01 = [5, -1, 3, 6, -7, 8, -11, 2]list02 = ['apple', 'pig', 'monkey', 'money']
print(sorted(list01))# print out: [-11, -7, -1, 2, 3, 5, 6, 8]
print(sorted(list01, key=abs))# print out: [-1, 2, 3, 5, 6, -7, 8, -11]
# 默认升序print(sorted(list02))# print out: ['apple', 'money', 'monkey', 'pig']
# 降序print(sorted(list02, reverse=True))# print out: ['pig', 'monkey', 'money', 'apple']
# 匿名函数排序print(sorted(list02, key=lambda x: len(x), reverse=True))# print out: ['monkey', 'apple', 'money', 'pig']
以上咱们简单的介绍了几个经常使用的高阶函数的使用,固然还有不少的高阶函数咱们能够去研究,好比 zip
函数等,但愿此节的介绍对你们有所帮助。
python 高阶函数[1]
[1]
python 高阶函数: https://github.com/JustDoPython/python-100-day/tree/master/day-005
系列文章
第11天:Python 字典