Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一系列函数:
functions = [function_a,function_b,square,...,last_function]
我想在一行的变量中获得 function_a(function_b(square(...(last_function("hello"))..))) 的输出,用列表理解技巧替换点。
像这样 :
tmp = input for f in functions: tmp = f(tmp) output = tmp
不使用 tmp,在一行中。
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