Finding the average of a list

前端 未结 23 1591
抹茶落季
抹茶落季 2020-11-22 11:07

I have to find the average of a list in Python. This is my code so far

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)
23条回答
  •  感情败类
    2020-11-22 11:40

    There is a statistics library if you are using python >= 3.4

    https://docs.python.org/3/library/statistics.html

    You may use it's mean method like this. Let's say you have a list of numbers of which you want to find mean:-

    list = [11, 13, 12, 15, 17]
    import statistics as s
    s.mean(list)
    

    It has other methods too like stdev, variance, mode, harmonic mean, median etc which are too useful.

提交回复
热议问题