Python基础11-函数式编程与内置函数

目录

函数即变量

lambda关键字定义匿名函数

高阶函数

内置函数map

内置函数filter

内置函数reduce

内置函数看文档


函数即变量

书接上回,Python里面,函数就是变量,可以被当成普通变量一样作为返回值,调用。

def foo():
    print("from foo")
    pass


def bar():
    print("from bar")
    return foo


t = bar()
t()
bar()()

甚至,可以返回函数自己。

def foo():
    print("from foo")
    return foo


foo()()()()()()

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)
        pass
    return 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)
        pass
    return 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)
        pass
    return res


# [1, 4, 9, 16]
print(num_square(n))

为此,我们可以将对列表元素的操作,作为函数参数传递给这个函数。这样,就可以使函数的操作灵活多样。

n = [1, 2, 3, 4]


def add_one(x):
    return x + 1


def sub_one(x):
    return x - 1


def square(x):
    return x ** 2


def num_op(n, op):
    res = []
    for i in n:
        res.append(op(i))
        pass
    return 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))
        pass
    return 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 reduce

a = [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 600

sum_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦行僧(csdn)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值