lambda表达式+reduce
Posted liuxuanhe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lambda表达式+reduce相关的知识,希望对你有一定的参考价值。
func=lambda x:x+1 func(1) Out[3]: 2
func=lambda x,y,z:x+1 func(1,2,3) Out[5]: 2
lambda可以输入任意多个变量。
func=lambda x,y:lambda z:z+1 func(1) Traceback (most recent call last): File "E:pythonlibsite-packagesIPythoncoreinteractiveshell.py", line 2963, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-9ed089a72395>", line 1, in <module> func(1) TypeError: <lambda>() missing 1 required positional argument: ‘y‘ func(1,2) Out[9]: <function __main__.<lambda>.<locals>.<lambda>(z)> func(1,2)(1) Out[10]: 2 func(2,3)(1) Out[11]: 2
由错误可知,第一个括号对应最外层lambda,第一个括号写入两个,只是传参,实例化了一个lambda对象,第二次才会调用函数,并计算值。
from functools import reduce print(reduce(lambda a,b: a+b ,[1,2,3,4])) 10
可以正常计算值,将传入的序列合并
from functools import reduce print(reduce(lambda a: a+1 ,[1,2,3,4])) Traceback (most recent call last): File "E:pythonlibsite-packagesIPythoncoreinteractiveshell.py", line 2963, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-16-246742737994>", line 1, in <module> print(reduce(lambda a: a+1 ,[1,2,3,4])) TypeError: <lambda>() takes 1 positional argument but 2 were given print(reduce(lambda a,b,c: a+b+c ,[1,2,3,4])) Traceback (most recent call last): File "E:pythonlibsite-packagesIPythoncoreinteractiveshell.py", line 2963, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-17-1dfc623ed0ba>", line 1, in <module> print(reduce(lambda a,b,c: a+b+c ,[1,2,3,4])) TypeError: <lambda>() missing 1 required positional argument: ‘c‘
reduce与lambda合用的那一层,只能传入两个参数,会合并传入的序列,所以传入两个值就够了
from functools import reduce def add(x): x = x+1 return x def mut(x): x = 2 ** x return x print(reduce(lambda f, g: lambda *a, **kw: g(f(*a, **kw)),[add,mut,add,mut])(1)) print(reduce(lambda f, g: lambda *a, **kw: f(g(*a, **kw)),[add,mut,add,mut])(1)) 32 9
*a, **kw:表示传入的参数,个数,类型都任意,一种通配的表示方法
第一种是按传入的序列执行顺序操作,先加再幂再加再幂。
第二种是按传入的序列执行逆序操作,先幂再加再幂再加。
from functools import reduce def test1(x,y,z): a=str(x) return a def test2(x): a = x.split(‘ ‘) return a def test3(x): b = [] print(type(x[0])) for i in x: print(type(i)) b.append(list(i)) print(i) return b print(reduce(lambda f, g: lambda x: (g(f(x))),[test1,test2,test3])([1,2,3],[234],1111)) Traceback (most recent call last): File "E:/1_python_code/code/python_1/test.py", line 220, in <module> print(reduce(lambda f, g: lambda x: (g(f(x))),[test1,test2,test3])([1,2,3],[234],1111)) TypeError: <lambda>() takes 1 positional argument but 3 were given from functools import reduce def test1(x,y,z): a=str(x) return a def test2(x): a = x.split(‘ ‘) return a def test3(x): b = [] print(type(x[0])) for i in x: print(type(i)) b.append(list(i)) print(i) return b print(reduce(lambda f, g: lambda x,y,z: (g(f(x,y,z))),[test1,test2,test3])([1,2,3],[234],11111)) <class ‘str‘> <class ‘str‘> [1, <class ‘str‘> 2, <class ‘str‘> 3] [[‘[‘, ‘1‘, ‘,‘], [‘2‘, ‘,‘], [‘3‘, ‘]‘]]
内层lambda传入的参数,与test1匹配
from functools import reduce def test1(x,y,z): print(‘test1‘) a=str(x) return a def test2(x): print(‘test2‘) a = x.split(‘ ‘) return a def test3(x): b = [] print(‘test3‘) print(type(x[0])) for i in x: print(type(i)) b.append(list(i)) print(i) return b print(reduce(lambda f, g: lambda x,y,z: (f(g(x,y,z))),[test1,test2,test3])([1,2,3],[234],11111)) Traceback (most recent call last): File "E:/1_python_code/code/python_1/test.py", line 223, in <module> print(reduce(lambda f, g: lambda x,y,z: (f(g(x,y,z))),[test1,test2,test3])([1,2,3],[234],11111)) File "E:/1_python_code/code/python_1/test.py", line 223, in <lambda> print(reduce(lambda f, g: lambda x,y,z: (f(g(x,y,z))),[test1,test2,test3])([1,2,3],[234],11111)) TypeError: test3() takes 1 positional argument but 3 were given Process finished with exit code 1 from functools import reduce def test1(x,y,z): print(‘test1‘) a=str(x) return a def test2(x): print(‘test2‘) a = x.split(‘ ‘) return a def test3(x): b = [] print(‘test3‘) print(type(x[0])) for i in x: print(type(i)) b.append(list(i)) print(i) return b print(reduce(lambda f, g: lambda x: (f(g(x))),[test1,test2,test3])([1,2,3])) test3 Traceback (most recent call last): <class ‘int‘> File "E:/1_python_code/code/python_1/test.py", line 223, in <module> <class ‘int‘> print(reduce(lambda f, g: lambda x: (f(g(x))),[test1,test2,test3])([1,2,3])) File "E:/1_python_code/code/python_1/test.py", line 223, in <lambda> print(reduce(lambda f, g: lambda x: (f(g(x))),[test1,test2,test3])([1,2,3])) File "E:/1_python_code/code/python_1/test.py", line 219, in test3 b.append(list(i)) TypeError: ‘int‘ object is not iterable Process finished with exit code 1
由两次的错误信息,及第二次错误的定位,更加肯定将函数内的f和g更换位置后,先执行了test3,是逆序执行。
以上是关于lambda表达式+reduce的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Lambda 表达式 .reduce() 方法减少给定列表
python之lambda,filter,map,reduce函数