lambda、map、reduce、filter函数讲解
# coding:utf-8 """ 几个特殊的函数: lambda lambda后面直接跟变量 变量后面是冒号 冒号后面是表达式,表达式计算结果就是本函数的返回值 作用:没有给程序带来性能上的提升,带来的是代码的简洁 map 格式:map(func, seq) func是一个函数,seq是一个序列对象 最终结果得到一个list 执行时,序列对象中的每个元素,从左到右的顺序,一次被取出来,并塞入到func那个函数中 map是上下运算 reduce reduce是横向逐个元素进行运算 filter 过滤器 """ # lambda 功能的三种实现方式 # 最原始方式 def lambda_test(): def add(x): x += 3 return x numbers = range(10) new_numbers = [] for i in numbers: new_numbers.append(add(i)) return new_numbers # 列表解析的方式,推荐使用 def lambda_test2(): return [i+3 for i in range(10)] # lambda方式实现 def lambda_test3(): lam = lambda x:x+3 # 一行表示了add方法 numbers = range(10) n2 = [] for i in numbers: n2.append(lam(i)) return n2 # lambda 多参数 def lambda_test4(x,y): g = lambda x,y:x+y #计算x+y print g #返回的是方法名地址 <function <lambda> at 0x0000000002A2AB38> return g(x,y) # lambda多参改进 def lambda_test5(x,y): return (lambda x,y:x+y)(x,y) """ map """ # lambda_test的功能也能通过map实现 def map_test(): def add(x): x += 3 return x numbers = range(10) return map(add, numbers) # map改进,lambda实现函数 def map_test2(): numbers = range(10) return map(lambda x:x+3, numbers) # 列表解析实现map的功能 def map_test3(): return [i+3 for i in range(10)] # map的优雅(多参) def map_test4(): list1 = range(1,6) list2 = range(6,11) return map(lambda x,y:x+y, list1, list2) # zip方式实现map_test4功能 def map_test5(): list1 = range(1,6) list2 = range(6,11) lst = zip(list1, list2) return [x+y for x,y in lst] # reduce def reduce_test(): return reduce(lambda x,y:x+y, range(10)) # for循环实现 def reduce_test2(): lam = lambda x,y:x+y numbers = range(10) sum_number = 0 for i in numbers: sum_number += i return sum_number # 列表解析器操作,不能复用,函数发生变化,列表解析器就失效了 def reduce_test3(): return sum([x for x in range(10)]) #练习 #两个list,a=[3,9,8,5,2],b=[1,4,9,2,6].计算a[0]b[0]+a[1]b[1]+...的结果 #方法1: def test(): a,b = [3,9,8,5,2],[1,4,9,2,6] lst = zip(a,b) return sum(x*y for x,y in lst) #方法2 def test2(): a,b = [3,9,8,5,2],[1,4,9,2,6] lst = zip(a,b) return reduce(lambda x,y:x+y, [m*n for m,n in lst]) # 方法3, lambda、map、reduce都使用上了 def test3(): a,b = [3,9,8,5,2],[1,4,9,2,6] return reduce(lambda x,y:x+y,map(lambda x,y:x*y, a,b)) #filter def filter_test(): numbers = range(-5,5) print numbers return filter(lambda x:x>0, numbers) # 列表解析器执行filter def filter_test2(): numbers = range(-5,5) return [x for x in numbers if x>0] if __name__ =="__main__": print "----lambda-------" print lambda_test() print lambda_test2() print lambda_test3() print lambda_test4(3,4) print lambda_test5(5,6) print "-----map-----" print map_test() print map_test2() print map_test3() print map_test4() print map_test5() print "-------reduce-------" print reduce_test() print reduce_test2() print reduce_test3() print "----exercise--------" print test() print test2() print test3() print "-----filter----------" print filter_test() print filter_test2()
执行的结果是:
----lambda------- [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] <function <lambda> at 0x0000000002A792E8> 7 11 -----map----- [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [3, 4, 5, 6, 7, 8, 9, 10, 11, 12] [7, 9, 11, 13, 15] [7, 9, 11, 13, 15] -------reduce------- 45 45 45 ----exercise-------- 133 133 133 -----filter---------- [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4]