我有一系列功能:
functions = [function_a,function_b,square,...,last_function]
我想在一行中的变量中获取function_a(function_b(square(...(last_function(“hello”))...)))的输出,用列表理解技巧替换点。 / p>
像这样:
tmp = input
for f in functions:
tmp = f(tmp)
output = tmp
不使用tmp,在一行中。
答案 0 :(得分:3)
your_value = 3
result = reduce(lambda x, y: y(x), function_list, your_value)
例如:
>>> functions = [lambda x: x + 2, lambda x: x * 2]
>>> reduce(lambda x, y: y(x), functions, 1)
6