![destination destination](https://c1.lmlphp.com/image/static/default_avatar.gif)
本文介绍了在Python中计算一系列函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从初始值中获得计算链的结果。我实际上使用下面的代码:
def function_composition(function_list,origin):
destination = origin
为函数列表中的函数:
destination = func(目的地)
返回目标
function_list 中有一个参数。
我想知道是否有类似的功能在Python标准库或更好的方式(例如:使用lambdas)来做到这一点。 解决方案
/ p>
destination = reduce((lambda x,y:y(x)),function_list,origin)
I want to get the result of a chain of computations from an initial value. I'm actually using the following code:
def function_composition(function_list, origin):
destination = origin
for func in function_list:
destination = func(destination)
return destination
With each function in function_list
having a single argument.
I'd like to know if there is a similar function in python standard library or a better way (example: using lambdas) to do this.
解决方案
Fold while calling.
destination = reduce((lambda x, y: y(x)), function_list, origin)
这篇关于在Python中计算一系列函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!