python 高阶函数之 reduce

1、正常写法

>>> from functools import reduce >>> def fn(x, y): ... return x * 10 + y ... >>> reduce(fn, [1, 3, 5, 7, 9]) 得到 13579

2、简写
reduce(lambda x, y: x * 10 + y,[1, 3, 5, 7, 9])

 步骤拆解:

  1当x, 3当y 1*10+3 =13
  13当x,5当y 13*10+5=135
  .....最终得到13579

原文地址:https://www.cnblogs.com/wdw31210/p/10559670.html