How does reduce function work?

前端 未结 9 1137
青春惊慌失措
青春惊慌失措 2020-12-01 04:25

As far as I understand, the reduce function takes a list l and a function f. Then, it calls the function f on first two elements of th

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 05:00

    You could also implement factorial using reduce.

    def factorial(n):
      return(reduce(lambda x,y:x*y,range(n+1)[1:]))
    

提交回复
热议问题