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
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