智一面的面试题提供python的测试题
http://www.gtalent.cn/exam/interview?token=f098f775ffa0106d0451f7fd97357f23
Lambda表达式
Lambda表达式又被称之为匿名函数
格式
lambda 参数列表:函数体
def add(x,y):
return x+y
print(add(3,4))
#上面的函数可以写成Lambda函数
add_lambda=lambda x,y:x+y
add_lambda(3,4)
map函数
函数就是有输入和输出,map的输入和输出对应关系如下图所示:

就是要把一个可迭代的对象按某个规则映射到新的对象上。
因此map函数要有两个参数,一个是映射规则,一个是可迭代对象。
list1=[1,2,3,4,5]
r=map(lambda x:x+x,list)
print(list1(r))
结果:[2,4,6,8,10]
m1=map(lambda x,y:x*x+y,[1,2,3,4,5],[1,2,3,4,5])
print(list(ml))
结果:[2,6,12,20,30]
filter函数
filter的输入和输出对应关系如下图所示:

def is_not_none(s):
return s and len(s.strip())>0
list2=['','','hello','xxxx', None,'ai']
result=filter(is_not_none, list2)
print(list(result))
结果:[‘hello’,‘xxxx’,‘ai’]
reduce函数

from functools import reduce
f=lambda x,y:x+y x=reduce(f,[1,2,3,4,5])
print(r)
结果:15=1+2+3+4+5
相当于每一次计算都是基于前一次计算的结果:

还可以为reduce计算添加初始值:
from functools import reduce
f=lambda x,y:x+y x=reduce(f,[1,2,3,4,5],10)
print(r)
结果:25=10+1+2+3+4+5
————————————————
我们的python技术交流群:941108876
智一面的面试题提供python的测试题
http://www.gtalent.cn/exam/interview?token=f098f775ffa0106d0451f7fd97357f23