Python3 中lambda函数、map函数、reduce函数总结学习

1. lambda函数

在Python手册中,对labmda函数是这样描述的。

lambda: An anonymous inline function consisting of a single expression which is evaluated when the function

is called. The syntax to create a lambda function is lambda [parameters]: expression

lambda函数是一个匿名的内置函数,其包含一个表达式且在使用这个函数的时候会进行估值计算。使用lambda函数的方式就是:

lambda 参数1, 参数2, 参数3: 表达式

= lambda x, y : x+y

f(1,2) # 结果为3

这里lambda作为一个表达式,定义了一个匿名函数,上例的代码x,y为入口参数,x+y为函数体,其结果就是返回x+y的和。其实我们在使用lambda函数的时候通常是将其放在一个代码式子中,省的单独再去定义一个函数.

 

2. map函数

map()是 Python 内置的高阶函数,它接收一个函数 f 和一个Iterable(必须可以由for循环的变量, 比如list, str等等),并通过把函数 f 依次作用在Iterable 的每个元素上,并把结果作为新的Iterator返回。比如:

问:如何将list列表中的字符转换为数值型?

s = "1234567"
s = list(str[1:5]) # 这里s实字符串类型
s = map(int, s) # 将s每个字符串转化成int类型,最后就转化成整数数字了
这也是将list中的每个字符串元素转换成整数了。

3. reduce函数
reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值。 
python3使用reduce需要import一下:
from functools import reduce
print(reduce(lambda x,y:x+y,range(1,101)))
reduce()还可以接收第3个可选参数,作为计算的初始值。如果把初始值设为888,计算:
print(reduce(lambda x,y:x+y,range(1,101),888))
例如用Python求1到100的和就可以简单的写成:
print(reduce(lambda x,y:x+y,range(1,101)))

参考:https://blog.csdn.net/katyusha1/article/details/81538944

4.filter函数

filter():简单的理解为过滤器,需要两个参数,function,和一个序列(字符串、列表、元组都是序列),过滤器会依次将序列的值传入function中,

    如果返回True的话,将其重新生成一个列表返回。

 list(filter(lambda x:True if x % 3 == 0 else False, range(100)))
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
5.map函数
map():映射,用法和filter()类似,也是将序列放入函数进行运算,但是,不论运算结果为什么,map()都将忠实反馈,这是map()和filter()的主要区别。请注意,filter()和map()中的function都必要有一个返回值。
list(map(lambda x:True if x % 3 == 0 else False, range(100)))
[True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True]
参考:https://www.cnblogs.com/jydeng/p/4145188.html


下面是一个运用的例子:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-05-19 21:08
# @Software: PyCharm
'''The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
'''
str = '73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'
from functools import reduce

delta = 13
lens = len(str)
max_num = 0
for i in range(lens - delta):
s = list(str[i: i + delta]) # 这里s实字符串类型
s = map(int, s) # 将s每个字符串转化成int类型
a = reduce(lambda x, y: x * y, s) # 字符串里面的所有数字相乘
if a > max_num:
max_num = a
print(max_num)
结果为:23514624000

 

posted on 2019-05-19 22:40  Tom_Mo  阅读(1522)  评论(0编辑  收藏  举报

导航