Finding the average of a list

前端 未结 23 1975
抹茶落季
抹茶落季 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:36

    Why would you use reduce() for this when Python has a perfectly cromulent sum() function?

    print sum(l) / float(len(l))
    

    (The float() is necessary to force Python to do a floating-point division.)

提交回复
热议问题