ypr*_*rez 509
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)
Run Code Online (Sandbox Code Playgroud)
Her*_*rms 496
如果你的减少已经归还你的金额,那么你剩下要做的就是分裂.
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import statistics
statistics.mean(l) # 20.11111111111111
Run Code Online (Sandbox Code Playgroud)
虽然statistics.mean()会更简单,因为你不需要lambda.
如果你想要一个更精确的float结果而不是int,那么只需使用len而不是reduce.
Aka*_*all 271
或者你可以使用numpy.mean:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import numpy as np
print(np.mean(l))
Run Code Online (Sandbox Code Playgroud)
Mar*_*agh 228
python 3.4中添加了一个统计模块.它有一个函数来计算所谓的平均平均值.您提供的列表的示例如下:
from statistics import mean
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(l)
Run Code Online (Sandbox Code Playgroud)
kin*_*all 44
reduce()当Python具有完美的sum()功能时,你为什么要使用它呢?
print sum(l) / float(len(l))
Run Code Online (Sandbox Code Playgroud)
(这float()是迫使Python进行浮点除法的必要条件.)
Che*_*rma 35
如果您使用python> = 3.4,则有一个统计库
https://docs.python.org/3/library/statistics.html
你可以使用像这样的平均方法.假设您有一个您想要找到的数字列表: -
list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)
Run Code Online (Sandbox Code Playgroud)
它还有其他方法,如stdev,方差,模式,调和均值,中位数等,这些方法太有用了.
Max*_*amy 18
您可以在总和中添加0.0,而不是强制转换为浮点数:
def avg(l):
return sum(l, 0.0) / len(l)
Run Code Online (Sandbox Code Playgroud)
And*_*ark 10
sum(l) / float(len(l)) 是正确的答案,但为了完整性,您可以通过单一减少来计算平均值:
>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114
Run Code Online (Sandbox Code Playgroud)
请注意,这可能会导致轻微的舍入错误:
>>> sum(l) / float(len(l))
20.111111111111111
Run Code Online (Sandbox Code Playgroud)
小智 8
我尝试使用上面的选项,但没有奏效.试试这个:
from statistics import mean
n = [11, 13, 15, 17, 19]
print(n)
print(mean(n))
Run Code Online (Sandbox Code Playgroud)
在python 3.5上工作
在效率和速度方面,这些是我测试其他答案的结果:
# test mean caculation
import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
LIST_RANGE = 10000000000
NUMBERS_OF_TIMES_TO_TEST = 10000
l = list(range(10))
def mean1():
return statistics.mean(l)
def mean2():
return sum(l) / len(l)
def mean3():
return np.mean(l)
def mean4():
return np.array(l).mean()
def mean5():
return reduce(lambda x, y: x + y / float(len(l)), l, 0)
def mean6():
return pd.Series(l).mean()
for func in [mean1, mean2, mean3, mean4, mean5, mean6]:
print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
Run Code Online (Sandbox Code Playgroud)
结果:
mean1 took: 0.17030245899968577
mean2 took: 0.002183011999932205
mean3 took: 0.09744236000005913
mean4 took: 0.07070840100004716
mean5 took: 0.022754742999950395
mean6 took: 1.6689282460001778
Run Code Online (Sandbox Code Playgroud)
很明显,赢家是:
sum(l) / len(l)
或使用pandas的Series.mean方法:
pd.Series(sequence).mean()
Run Code Online (Sandbox Code Playgroud)
演示:
>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>>
Run Code Online (Sandbox Code Playgroud)
从文档:
Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)¶
这是文档:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html
以及整个文档:
我在 Udacity\xc2\xb4s 问题中有一个类似的问题需要解决。而不是我编码的内置函数:
\n\ndef list_mean(n):\n\n summing = float(sum(n))\n count = float(len(n))\n if n == []:\n return False\n return float(summing/count)\nRun Code Online (Sandbox Code Playgroud)\n\n比平时要长得多,但对于初学者来说相当具有挑战性。
\n作为初学者,我刚刚编写了这样的代码:
L = [15, 18, 2, 36, 12, 78, 5, 6, 9]
total = 0
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
print average(L)
Run Code Online (Sandbox Code Playgroud)
如果您想获得的不仅仅是平均值(又名平均值),您可以查看 scipy stats:
from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))
# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111,
# variance=572.3611111111111, skewness=1.7791785448425341,
# kurtosis=1.9422716419666397)
Run Code Online (Sandbox Code Playgroud)