compute mean in python for a generator

后端 未结 10 2264
遇见更好的自我
遇见更好的自我 2021-01-01 21:58

I\'m doing some statistics work, I have a (large) collection of random numbers to compute the mean of, I\'d like to work with generators, because I just need to compute the

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 22:49

    You can use reduce without knowing the size of the array:

    from itertools import izip, count
    reduce(lambda c,i: (c*(i[1]-1) + float(i[0]))/i[1], izip(values,count(1)),0)
    

提交回复
热议问题