[英]Compute a chain of functions in python
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. 与每个功能
function_list
具有一个参数。
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. 我想知道python标准库中是否有类似的函数或更好的方法(例如:使用lambdas)来执行此操作。
打电话时折叠。
destination = reduce((lambda x, y: y(x)), function_list, origin)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.