compute mean in python for a generator

后端 未结 10 2231
遇见更好的自我
遇见更好的自我 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:48

    The old-fashioned way to do it:

    def my_mean(values):
       sum, n = 0, 0
       for x in values:
          sum += x
          n += 1
       return float(sum)/n
    

提交回复
热议问题