零、python的lambda函数:
1 #lambda function 2 func = lambda x : x+1 3 #这里是一个匿名函数,x是参数,x+1是对参数的操作 4 func(1)= 2
多个参数的lambda如下:
1 func = lambda x,y,x : x+y+z 2 #above 3 func(1,2,3) = 6
一、python的map函数:
1 #function define abs 2 def abs(x): 3 return x if x > 0 else -x 4 #function map 5 map(abs,[1,2,-1,-7]) = [1,2,1,7] 6 #遍历后面的参数liist 每一个都传入前面的函数名中运算得出新的list
二、python的reduce(遇到过坑,这是二元的,函数只能是两个参数的):
1 #function define 2 def add(x,y): 3 return x+y 4 #reduce 5 reduce(add,[1,2,3,4,5,6,7,8,9]) = 45 6 # 1+2+3+4+5+6+7+8+9= 45
三、python的filter()--》把参数的list的按照前面的函数算法过滤:
1 #filter 2 #define function 3 a = [1,2,3,4,5,6,7,8] 4 filter(lambda x:x>5,a) = [6,7,8]
四、自定义函数名作为参数传入调用:
1 #define 2 def test(p1,p2,p3,p4): 3 return (p1,p2,p3,p4) 4 def func_select(funcname,para): 5 print funcname(para[0],para[1],para[2],para[4]) 6 para = (p1,p2,p3,p4) 7 func_select(test,para)