Docstring:reduce(function, sequence[, initial])-> value
Apply a function of two arguments cumulatively to the items of a sequence,from left to right, so as to reduce the sequence to a single value.
For example,reduce(lambda x, y: x+y,[1,2,3,4,5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation,and serves as a default when the
sequence is empty.
Type: builtin_function_or_method
可迭代对象sequence不能为空
初始值initial没提供就在可迭代对象中取一个元素
1.2 示例
from functools importreduceprint(reduce(lambda x,y: x + y,range(1,5),10))# 20# 10 + 1# 11 + 2# 13 + 3# 16 + 4# 20print(reduce(lambda x,y: x * y,range(1,5),10))# 240
from functools importreduce
nums =[6,9,1,2,10,5]print(nums)print(sum(nums))print(reduce(lambda val, x: val + x, nums))print(reduce(lambda val, x: val + x, nums,10))
Init signature: partial(self,/,*args,**kwargs)
Docstring:
partial(func,*args,**keywords)- new function with partial application
of the given arguments and keywords.