目录
函数即变量
lambda关键字定义匿名函数
高阶函数
内置函数map
内置函数filter
内置函数reduce
内置函数看文档
函数即变量
书接上回,Python里面,函数就是变量,可以被当成普通变量一样作为返回值,调用。
def foo():print("from foo")passdef bar():print("from bar")return foot = bar()
t()
bar()()
甚至,可以返回函数自己。
def foo():print("from foo")return foofoo()()()()()()
lambda关键字定义匿名函数
用lambda关键字,可以定义函数,而没有函数名。这里为了举例方便,我们将lambda定义的函数赋值给一个变量。但是实际上,lambda表达式不是这么用的。
foo = lambda x: x + 1
t = foo(2)
print(t)
lambda定义匿名函数,可以返回多个值,但是要手动写成元组的形式。
foo = lambda x, y, z: (x + 1, y + 1, z + 1,)
t = foo(1, 2, 3)
print(t)
# (2, 3, 4)
高阶函数
函数的参数是函数名或者函数的返回值是函数名,这个函数就是高阶函数。
比如,需要写一个函数可以对一个列表的元素做操作。这个函数的操作是固定的,如果操作变了,那么要重新定义新的函数。
n = [1, 2, 3, 4]def num_add(n):"""对列表每个元素加1:param n::return:"""res = []for i in n:res.append(i + 1)passreturn res# [2, 3, 4, 5]
print(num_add(n))def num_sub(n):"""对列表每个元素减1:param n::return:"""res = []for i in n:res.append(i - 1)passreturn res# [0, 1, 2, 3]
print(num_sub(n))def num_square(n):"""对列表每个元素平方:param n::return:"""res = []for i in n:res.append(i ** 2)passreturn res# [1, 4, 9, 16]
print(num_square(n))
为此,我们可以将对列表元素的操作,作为函数参数传递给这个函数。这样,就可以使函数的操作灵活多样。
n = [1, 2, 3, 4]def add_one(x):return x + 1def sub_one(x):return x - 1def square(x):return x ** 2def num_op(n, op):res = []for i in n:res.append(op(i))passreturn res# [2, 3, 4, 5]
# [0, 1, 2, 3]
# [1, 4, 9, 16]
print(num_op(n, add_one))
print(num_op(n, sub_one))
print(num_op(n, square))
甚至,我们可以传入一个lambda表达式,省去定义函数的步骤。
n = [1, 2, 3, 4]def num_op(n, op):res = []for i in n:res.append(op(i))passreturn res# [2, 3, 4, 5]
# [0, 1, 2, 3]
# [1, 4, 9, 16]
print(num_op(n, lambda x: x + 1))
print(num_op(n, lambda x: x - 1))
print(num_op(n, lambda x: x ** 2))
内置函数map
对于上面所说高阶函数,有一个Python内置函数map可以简单的实现上述功能。并且是函数式编程哦。
map接收两个参数,第一个是函数(一般传入匿名函数),第二个是迭代器,返回迭代器。map函数对传入的迭代器遍历,用传入的函数逐个处理,返回迭代器需要自己转换成需要的数据类型。
n = [1, 2, 3, 4]
res1 = list(map(lambda x: x + 1, n))
res2 = list(map(lambda x: x - 1, n))
res3 = list(map(lambda x: x ** 2, n))
# [2, 3, 4, 5]
# [0, 1, 2, 3]
# [1, 4, 9, 16]
print(res1, res2, res3, sep="\n")# 用map函数对多个字符串列表做对应拼接
s1 = ['cn', 'us', 'uk']
s2 = ['ca', 'jp', 'kk']
s3 = ['11', '22', '33']res = list(map(lambda x, y, z: x + y + z, s1, s2, s3))
print(res)
# ['cnca11', 'usjp22', 'ukkk33']
内置函数filter
filter的逻辑和map类似,功能和名字filter一样——过滤。filter接收两个参数,第一个是函数(一般是匿名函数,返回值是布尔值),第二个是迭代器,返回迭代器。filter函数对传入的迭代器遍历,用传入的函数逐个判断,如果是True保留,如果是False去掉,返回迭代器需要自己转换成需要的数据类型。
# 找出下面列表里包含字母g的字符串
s = ["Able", "Baker", "Charlie", "Dog", "Easy", "Fox", "George"]
res = list(filter(lambda x: x.casefold().find("g") != -1, s))
print(res)
# ['Dog', 'George']
筛选操作系统是linux,数据库是mysql的系统。
system = [{"os": "windows", "db": "oracle", "app": "sap"},{"os": "linux", "db": "mysql", "app": "apache"},{"os": "windows", "db": "db2", "app": "nginx"},{"os": "linux", "db": "oracle", "app": "sap"},
]# 过滤出操作系统是linux,数据库是mysql的系统
res = list(filter(lambda x: x.get("os") == "linux" and x.get("db") == "mysql", system))
print(res)
# [{'os': 'linux', 'db': 'mysql', 'app': 'apache'}]
内置函数reduce
在Python3以后,reduce函数被放在functools包里面,如果要使用的话需要from functools import reduce。
reduce对于迭代器做操作,接收三个参数,第一个是函数(一般是匿名函数),第二个是迭代器,第三个是可选参数给定的初始值,返回一个结果。以对数列求和、求积为例。
from functools import reducea = [1, 2, 3, 100]sum_a = reduce(lambda x, y: x + y, a)
multi_a = reduce(lambda x, y: x * y, a)
print(sum_a, multi_a)
# 106 600sum_a = reduce(lambda x, y: x + y, a, 100)
multi_a = reduce(lambda x, y: x * y, a, 100)
print(sum_a, multi_a)
# 206 60000
内置函数看文档
Python的内置函数还有很多,不可能一一描述,最好是看官方文档。
下载Docs的地址是https://docs.python.org/3/download.html