Lambda 算子、过滤、缩减和映射,2024年大厂程序员进阶宝典

之前定义的 map_functions 函数可以使用列表推导技术来简化,我们将在章节列表推导中介绍:

def map_functions(x, 函数):

返回 [ func(x) for func in 函数 ]

过滤

功能

过滤器(函数,序列)

提供了一种优雅的方法来过滤掉序列“序列”的所有元素,函数函数返回True。即,如果 item 包含在序列“sequence”中并且 function(item) 返回 True,则 filter(function, sequence) 的迭代器结果将生成一个 item。

换句话说:函数 filter(f,l) 需要一个函数 f 作为它的第一个参数。f 必须返回一个布尔值,即 True 或 False。此函数将应用于列表 l 的每个元素。只有 f 返回 True 元素才会被迭代器产生,也就是 filter(function, sequence) 的返回值。

在下面的例子中,我们首先过滤掉前 11 个斐波那契数列的奇数和偶数元素:

fibonacci = [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ]

odd_numbers = list ( filter ( lambda x : x % 2 , fibonacci ))

打印( odd_numbers )

输出:

[1, 1, 3, 5, 13, 21, 55]

even_numbers = list ( filter ( lambda x : x % 2 == 0 , fibonacci ))

打印( even_numbers )

输出:

[0, 2, 8, 34]

even_numbers = list ( filter ( lambda x : x % 2 - 1 , fibonacci ))

打印( even_numbers )

输出:

[0, 2, 8, 34]

减少列表


正如我们在教程本章的介绍中提到的。在迁移到 Python 3 时,reduce() 已从 Python 的核心中删除。 Guido van Rossum 讨厌 reduce(),我们可以从他在 2005 年 3 月 10 日在 artima.com 上发表的声明中了解到:

"So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly."

功能

减少(功能,序列)

不断地将函数 func() 应用于序列 seq。它返回单个值。

如果 seq = [ s1, s2, s3, … , sn ],调用 reduce(func, seq) 的工作方式如下:

At first the first two elements of seq will be applied to func, i.e. func(s1,s2) The list on which reduce() works looks now like this: [ func(s1, s2), s3, ... , sn ] In the next step func will be applied on the previous result and the third element of the list, i.e. func(func(s1, s2),s3) The list looks like this now: [ func(func(s1, s2),s3), ... , sn ] Continues like this until just one element is left and returns this element as the result of reduce()

如果 n 等于 4,前面的解释可以这样说明:

减少

我们想用一个简单的例子来说明 reduce() 的这种工作方式。我们必须导入 functools 才能使用 reduce:

导入 functools

functools 。减少( lambda x , y : x + y , [ 47 , 11 , 42 , 13 ])

输出:

113

下图显示了计算的中间步骤:

减少:操作方法

reduce() 的例子


使用 reduce 确定数值列表的最大值:

from functools import reduce

f = lambda a , b : a if ( a > b ) else b

reduce ( f , [ 47 , 11 , 42 , 102 , 13 ])

输出:

102

计算从 1 到 100 的数字之和:

from functools import reduce

reduce ( lambda x , y : x + y , range ( 1 , 101 ))

输出:

5050

将前面的示例更改为计算从 1 到数字的乘积(阶乘)非常简单,但不要选择 100。我们只需要将“+”运算符变成“*”:

减少( lambda x , y : x * y , 范围( 1 , 49 ))

输出:

12413915592536072670862289047373375038521486354677760000000000

如果您喜欢彩票,则有机会赢得 49 次抽奖中的 6 次:

减少( lambda x , y : x * y , 范围( 44 , 50 )) /减少( lambda x , y : x * y , 范围( 1 , 7 ))

输出:

13983816.0

练习

  1. 想象一下书店中使用的会计程序。它适用于带有子列表的列表,如下所示:

Order Number Book Title and Author Quantity Price per Item 34587 Learning Python, Mark Lutz 4 40.95 98762 Programming Python, Mark Lutz 5 56.80 77226 Head First Python, Paul Barry 3 32.95 88112 Einführung in Python3, Bernd Klein 3 24.99 Write a Python program, which returns a list with 2-tuples. Each tuple consists of a the order number and the product of the price per items and the quantity. The product should be increased by 10,- € if the value of the order is smaller than 100,00 €. Write a Python program using lambda and map.

  1. 同一个书店,但这次我们在不同的清单上工作。我们列表的子列表看起来像这样: [ordernumber, (article number,quantity, price per unit), … (article number,quantity, price per unit) ] 编写一个程序,它返回一个包含 (order数量,订单总额)。

习题解答


订单 = [ [ “34587” , “Python入门,马克鲁兹” , 4 , 40.95 ],

[ “98762” ,“ 编程的Python,马克鲁兹” , 5 , 56.80 ],

[ “77226” , “头第一个Python,保Barry" , 3 , 32.95 ],

[ “88112” , “Einführung in Python3, Bernd Klein” , 3 , 24.99 ]]

min_order = 100

invoice_totals = 列表地图( lambda x : x if x [ 1 ] >= min_order else ( x [ 0 ], x [ 1 ] + 10 ),

map ( lambda x : ( x [ 0 ], x [ 2 ] * x [ 3 ]), 订单)))

打印(invoice_totals )

输出:

[(‘34587’, 163.8), (‘98762’, 284.0), (‘77226’, 108.850000000000001), (‘88112’, 84.97)]

functools 导入 减少

订单 = [ [ 1 , (“5464” , 4 , 9.99 ), (“8274” ,18 ,12.99 ), (“9744” , 9 , 44.95 )],

[ 2 , (“5464” , 9 , 9.99 ), ( “9744” , 9 , 44.95 )],

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
img

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值