Reduce function with three parameters

前端 未结 3 1177
谎友^
谎友^ 2020-12-10 01:51

How does reduce function work in python3 with three parameters instead of two. So, for two,

tup = (1,2,3)
reduce(lambda x, y: x+y, tup)
<         


        
3条回答
  •  半阙折子戏
    2020-12-10 02:20

    Providing a tuple as a third parametr we will be able to calculate and return from reduce multiple values.

    from functools import reduce
    def mean(my_list):                  # == sum(my_list) / len(my_list)
        return (lambda x: x[0]/x[1]) (reduce(lambda x, y : (x[0]+y, x[1]+1), 
                                                    my_list, (0, 0,)))
    

提交回复
热议问题