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

290 篇文章 0 订阅

def map_functions(x, 函数):

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

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

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

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

[0, 2, 8, 34]

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

[0, 2, 8, 34]

"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."

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()

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

113

reduce() 的例子

from functools import reduce

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

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

102

from functools import reduce

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

5050

12413915592536072670862289047373375038521486354677760000000000

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数量，订单总额）。

[ “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 ]), 订单)))

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

functools 导入 减少

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

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

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

• 27
点赞
• 20
收藏
觉得还不错? 一键收藏
• 0
评论
09-24 1万+
08-04 1426
05-29 1万+
08-14 1万+
12-19 398
06-12 1万+
09-18 1004
06-13 1114
06-14 558
06-17 132

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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