Python减少解释 [英] Python reduce explanation
本文介绍了Python减少解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法理解以下代码段:
I'm not able to understand the following code segment:
>>> lot = ((1, 2), (3, 4), (5,))
>>> reduce(lambda t1, t2: t1 + t2, lot)
(1, 2, 3, 4, 5)
reduce函数如何生成(1,2,3,4,5)的元组?
How does the reduce function produce a tuple of (1,2,3,4,5) ?
推荐答案
如果将 lambda
分解为一个函数,会更容易,所以它更清晰:
It's easier if you break out the lambda
into a function, so it's clearer to what's going on:
>>> def do_and_print(t1, t2):
print 't1 is', t1
print 't2 is', t2
return t1+t2
>>> reduce(do_and_print, ((1,2), (3,4), (5,)))
t1 is (1, 2)
t2 is (3, 4)
t1 is (1, 2, 3, 4)
t2 is (5,)
(1, 2, 3, 4, 5)
这篇关于Python减少解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文