问题:在Python中计算算术平均值(一种平均值)
Python中是否有内置或标准库方法来计算数字列表的算术平均值(一种平均值)?
Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers?
回答 0
我不知道标准库中的任何内容。但是,您可以使用类似以下内容的方法:
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0
在numpy中,有numpy.mean()
。
I am not aware of anything in the standard library. However, you could use something like:
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()
.
回答 1
NumPy的a numpy.mean
是算术平均值。用法很简单:
>>> import numpy
>>> a = [1, 2, 4]
>>> numpy.mean(a)
2.3333333333333335
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
回答 2
用途statistics.mean
:
import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335
从Python 3.4开始可用。对于3.1-3.3用户,该模块的旧版本可在PyPI上以的名称获得stats
。只需更改statistics
为即可stats
。
Use statistics.mean
:
import statistics
print(statistics.mean([1,2,4])) # 2.3333333333333335
It’s available since Python 3.4. For 3.1-3.3 users, an old version of the module is available on PyPI under the name stats
. Just change statistics
to stats
.
回答 3
您甚至不需要麻木或肮脏的…
>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3
You don’t even need numpy or scipy…
>>> a = [1, 2, 3, 4, 5, 6]
>>> print(sum(a) / len(a))
3
回答 4
使用scipy:
import scipy;
a=[1,2,4];
print(scipy.mean(a));
Use scipy:
import scipy;
a=[1,2,4];
print(scipy.mean(a));
回答 5
除了强制浮动之外,您还可以执行以下操作
def mean(nums):
return sum(nums, 0.0) / len(nums)
或使用lambda
mean = lambda nums: sum(nums, 0.0) / len(nums)
更新日期:2019-12-15
Python 3.8 在统计模块中添加了功能fmean。哪个更快,并且总是返回float。
将数据转换为浮点并计算算术平均值。
它的运行速度比mean()函数快,并且始终返回浮点数。数据可以是序列或可迭代的。如果输入数据集为空,则引发StatisticsError。
fmean([3.5,4.0,5.25])
4.25
3.8版的新功能。
Instead of casting to float you can do following
def mean(nums):
return sum(nums, 0.0) / len(nums)
or using lambda
mean = lambda nums: sum(nums, 0.0) / len(nums)
UPDATES: 2019-12-15
Python 3.8 added function fmean to statistics module. Which is faster and always returns float.
Convert data to floats and compute the arithmetic mean.
This runs faster than the mean() function and it always returns a
float. The data may be a sequence or iterable. If the input dataset is
empty, raises a StatisticsError.
fmean([3.5, 4.0, 5.25])
4.25
New in version 3.8.
回答 6
from statistics import mean
avarage=mean(your_list)
例如
from statistics import mean
my_list=[5,2,3,2]
avarage=mean(my_list)
print(avarage)
结果是
3.0
from statistics import mean
avarage=mean(your_list)
for example
from statistics import mean
my_list=[5,2,3,2]
avarage=mean(my_list)
print(avarage)
and result is
3.0
回答 7
def avg(l):
"""uses floating-point division."""
return sum(l) / float(len(l))
例子:
l1 = [3,5,14,2,5,36,4,3]
l2 = [0,0,0]
print(avg(l1)) # 9.0
print(avg(l2)) # 0.0
def avg(l):
"""uses floating-point division."""
return sum(l) / float(len(l))
Examples:
l1 = [3,5,14,2,5,36,4,3]
l2 = [0,0,0]
print(avg(l1)) # 9.0
print(avg(l2)) # 0.0
回答 8
def list_mean(nums):
sumof = 0
num_of = len(nums)
mean = 0
for i in nums:
sumof += i
mean = sumof / num_of
return float(mean)
def list_mean(nums):
sumof = 0
num_of = len(nums)
mean = 0
for i in nums:
sumof += i
mean = sumof / num_of
return float(mean)
回答 9
我一直认为应该avg
从Builtins / stdlib中省略它,因为它很简单
sum(L)/len(L) # L is some list
并且任何告诫将在调用者代码中解决以供本地使用。
注意事项:
非浮点结果:在python2中,9/4为2。解析,使用float(sum(L))/len(L)
或from __future__ import division
除以零:列表可能为空。解决:
if not L:
raise WhateverYouWantError("foo")
avg = float(sum(L))/len(L)
I always supposed avg
is omitted from the builtins/stdlib because it is as simple as
sum(L)/len(L) # L is some list
and any caveats would be addressed in caller code for local usage already.
Notable caveats:
non-float result: in python2, 9/4 is 2. to resolve, use float(sum(L))/len(L)
or from __future__ import division
division by zero: the list may be empty. to resolve:
if not L:
raise WhateverYouWantError("foo")
avg = float(sum(L))/len(L)
回答 10
对您问题的正确答案是使用statistics.mean
。但是为了好玩,这是一个不使用该len()
功能的Mean的版本,因此statistics.mean
可以在不支持该功能的生成器上使用它(如)len()
:
from functools import reduce
from operator import truediv
def ave(seq):
return truediv(*reduce(lambda a, b: (a[0] + b[1], b[0]),
enumerate(seq, start=1),
(0, 0)))
The proper answer to your question is to use statistics.mean
. But for fun, here is a version of mean that does not use the len()
function, so it (like statistics.mean
) can be used on generators, which do not support len()
:
from functools import reduce
from operator import truediv
def ave(seq):
return truediv(*reduce(lambda a, b: (a[0] + b[1], b[0]),
enumerate(seq, start=1),
(0, 0)))
回答 11
其他人已经发布了很好的答案,但有些人可能仍在寻找找到Mean(avg)的经典方法,因此,我在这里发布了此信息(在Python 3.6中测试过的代码):
def meanmanual(listt):
mean = 0
lsum = 0
lenoflist = len(listt)
for i in listt:
lsum += i
mean = lsum / lenoflist
return float(mean)
a = [1, 2, 3, 4, 5, 6]
meanmanual(a)
Answer: 3.5
Others already posted very good answers, but some people might still be looking for a classic way to find Mean(avg), so here I post this (code tested in Python 3.6):
def meanmanual(listt):
mean = 0
lsum = 0
lenoflist = len(listt)
for i in listt:
lsum += i
mean = lsum / lenoflist
return float(mean)
a = [1, 2, 3, 4, 5, 6]
meanmanual(a)
Answer: 3.5