Python3 lambda 函数入门示例 Python lambda 函数

Python lambda 函数

首先,这个语法跟C++的语法几乎一样;

通常称 lambda 函数为匿名函数 ,也称为 丢弃函数,因为应一下子就不要了,不会长期凝结下来形成SDK API;本人觉得它有点类似 inline 函数,或者叫做 小小函数,一行写罢;

一, 先看一眼示例

先运行要给简单的例子,让问题具象一些:

例一: x+x+x

#######################

python 复制代码
(base) hipper@hipper-G21:~$ python

Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>>

>>>

>>> triple = lambda x: x + x + x

>>> triple(3)

9

>>> print(triple(2))

6

>>>

#######################

例二:三维空间欧氏距离

#######################

python 复制代码
>>> import math

>>> eucli = lambda x, y, z: math.sqrt(x**2 + y**2 + z**2)

>>> eucli(3,4,0)

5.0

>>>

#######################

其中,这里的triple 和 eucli 是lambda 函数对象的指针;

二,lambda函数出现的场景

那么,lambda函数用在什么场景呢?

1,在 def 定义的函数内部

#######################

python 复制代码
import math


def add_x_y_z(x, y, z):

    add = lambda a, b: a+b

    sum = add(x, y)

    sum = add(sum, z)


    return sum


print( add_x_y_z(3, 4, 5))

#######################math 没用到

2,lambda 结合 filter

filter函数,顾名思义是对list中的每个元素做过滤,并返回一个新的list;

从数学考试得分list中,找出优秀的分数:

#######################

python 复制代码
(base) hipper@hipper-G21:~/ex/ex_python/lambda_ex$ ipython

Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]

Type 'copyright', 'credits' or 'license' for more information

IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.


In [1]: score_list=[77, 65, 47, 83, 77, 97, 89, 51, 92]


In [2]: outstanding_list=list(filter(lambda score: (score>80), score_list))


In [3]: outstanding_list

Out[3]: [83, 97, 89, 92]


In [4]:

#######################

3, lambda 结合 map

map函数,会把list中的元素一一作为参数,返回值一一构成新的列表;

#######################

python 复制代码
(base) hipper@hipper-G21:~$ ipython

Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]

Type 'copyright', 'credits' or 'license' for more information

IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.


In [1]: num_list=[1,2,3,4,5,6,7]


In [2]: is_even_list=list( map( (lambda num:(num%2==0)) , num_list ) )


In [3]: is_even_list

Out[3]: [False, True, False, True, False, True, False]


In [4]:

#######################

4, reduce 与 lambda结合

reduce函数在包functools 中,按照某个运算符一一累算 list中的所有元素

#######################

python 复制代码
(base) hipper@hipper-G21:~$ ipython

Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]

Type 'copyright', 'credits' or 'license' for more information

IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.


In [1]: from functools import reduce


In [2]: num_list=[1,2,3,4,5]


In [3]: sigma=reduce(lambda a1, a2: a1+a2, num_list)


In [4]: sigma

Out[4]: 15


In [5]: order=reduce(lambda a1, a2: a1*a2, num_list)


In [6]:

#######################

相关推荐
兵慌码乱5 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵7 小时前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio11 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户03321266636712 小时前
使用 Python 从零创建 Word 文档
python
Csvn16 小时前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽18 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户5569188175319 小时前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
兵慌码乱1 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei1 天前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python