def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0
In numpy, there's numpy.mean().
NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this:
>>> import numpy
>>> a = [1, 2, 4]
>>> numpy.mean(a)
2.3333333333333335
import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335
You don't even need numpy or scipy...
>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3
Use scipy:
import scipy;
a=[1,2,4];
print(scipy.mean(a));