求列表的平均值

如何在Python中找到列表的平均值?

[1, 2, 3, 4]  ⟶  2.5
1418658 次浏览

对于Python 3.8+,使用statistics.fmean来保持浮点数的数值稳定性。(快)。

对于Python 3.4+,使用statistics.mean来保持浮点数的数值稳定性。(慢)。

xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]


import statistics
statistics.mean(xs)  # = 20.11111111111111

对于较旧版本的Python 3,请使用

sum(xs) / len(xs)

对于Python 2,将len转换为浮点数以获得浮点除法:

sum(xs) / float(len(xs))
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(xs) / len(xs)

当Python有一个完美的cromulent sum()函数时,为什么要使用reduce()呢?

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

(在Python 2中,float()是强制Python执行浮点除法所必需的。)

print reduce(lambda x, y: x + y, l)/(len(l)*1.0)

或者像之前写的那样

sum(l)/(len(l)*1.0)

1.0是为了确保你得到一个浮点除法

为了使用reduce获取运行平均值,您需要跟踪到目前为止看到的元素总数。因为它在列表中不是一个简单的元素,你还必须给reduce传递一个额外的参数来折叠。

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111

sum(l) / float(len(l))是正确答案,但为了完整起见,你可以用一个简化来计算平均值:

>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)
20.111111111111114

注意,这可能会导致轻微的舍入误差:

>>> sum(l) / float(len(l))
20.111111111111111

使用# EYZ1:

xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]


import numpy as np
print(np.mean(xs))
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]


l = map(float,l)
print '%.2f' %(sum(l)/len(l))

对于Python 3.4 +,使用新的statistics模块中的mean()来计算平均值:

from statistics import mean
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(xs)

而不是强制转换为float,你可以将0.0加到和:

def avg(l):
return sum(l, 0.0) / len(l)

在Udacity的问题中,我也有一个类似的问题要解决。而不是一个内置的函数,我编码:

def list_mean(n):


summing = float(sum(n))
count = float(len(n))
if n == []:
return False
return float(summing/count)

比平时长得多,但对于初学者来说,这是相当具有挑战性的。

两者都可以在一个整数或至少10个十进制值上给出接近的值。但如果你真的考虑长浮动值,这两者可能是不同的。方法可以根据你想要达到的目标而有所不同。

>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> print reduce(lambda x, y: x + y, l) / len(l)
20
>>> sum(l)/len(l)
20

浮动值

>>> print reduce(lambda x, y: x + y, l) / float(len(l))
20.1111111111
>>> print sum(l)/float(len(l))
20.1111111111

@Andrew Clark的说法是正确的。

我尝试使用上面的选项,但没有工作。 试试这个:

from statistics import mean


n = [11, 13, 15, 17, 19]


print(n)
print(mean(n))

使用过python 3.5

结合上面的几个答案,我提出了以下与reduce一起工作的方法,并且不假设你在reduce函数中有L可用:

from operator import truediv


L = [15, 18, 2, 36, 12, 78, 5, 6, 9]


def sum_and_count(x, y):
try:
return (x[0] + y, x[1] + 1)
except TypeError:
return (x + y, 2)


truediv(*reduce(sum_and_count, L))


# prints
20.11111111111111

作为初学者,我只是编写了这个代码:

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)

我想补充另一种方法

import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)
numbers = [0,1,2,3]


numbers[0] = input("Please enter a number")


numbers[1] = input("Please enter a second number")


numbers[2] = input("Please enter a third number")


numbers[3] = input("Please enter a fourth number")


print (numbers)


print ("Finding the Avarage")


avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4


print (avarage)

假设

x = [
[-5.01,-5.43,1.08,0.86,-2.67,4.94,-2.51,-2.25,5.56,1.03],
[-8.12,-3.48,-5.52,-3.78,0.63,3.29,2.09,-2.13,2.86,-3.33],
[-3.68,-3.54,1.66,-4.11,7.39,2.08,-2.59,-6.94,-2.26,4.33]
]

你可以注意到x的尺寸是3*10,如果你需要得到mean到每一行,你可以输入这个

theMean = np.mean(x1,axis=1)

别忘了import numpy as np

如果您使用的是python >= 3.4,则有一个统计库

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

你可以像这样使用它的mean方法。让我们假设你有一个数字列表,你想找到平均值:-

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

它还有其他方法,比如stdev,方差,模式,调和平均值,中位数等,这些方法都非常有用。

如果你想要的不仅仅是平均值(又名平均),你可以看看scipy的统计:

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)

或者使用pandasSeries.mean方法:

pd.Series(sequence).mean()

演示:

>>> import pandas as pd
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> pd.Series(l).mean()
20.11111111111111
>>>

从文档中可以看出:

# EYZ0¶

这里是这个的文档:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html

整个文档:

https://pandas.pydata.org/pandas-docs/stable/10min.html

在列表中查找平均值 通过使用以下PYTHON代码:

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))

试着简单一点。

编辑:

我添加了另外两种获取列表平均值的方法(仅适用于Python 3.8+)。下面是我做的比较:

import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
import math


LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000


l = list(range(LIST_RANGE))


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()




def mean7():
return statistics.fmean(l)




def mean8():
return math.fsum(l) / len(l)




for func in [mean1, mean2, mean3, mean4, mean5, mean6, mean7, mean8 ]:
print(f"{func.__name__} took: ",  timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))

以下是我得到的结果:

mean1 took:  0.09751558300000002
mean2 took:  0.005496791999999973
mean3 took:  0.07754683299999998
mean4 took:  0.055743208000000044
mean5 took:  0.018134082999999968
mean6 took:  0.6663848750000001
mean7 took:  0.004305374999999945
mean8 took:  0.003203333000000086

有趣!看起来math.fsum(l) / len(l)是最快的方法,然后是statistics.fmean(l),最后是sum(l) / len(l)。好了!

感谢阿斯克勒庇俄斯为我展示了另外两种方式!


旧的回答:

就效率和速度而言,以下是我测试其他答案的结果:

# test mean caculation


import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd


LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000


l = list(range(LIST_RANGE))


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))

结果是:

mean1 took:  0.17030245899968577
mean2 took:  0.002183011999932205
mean3 took:  0.09744236000005913
mean4 took:  0.07070840100004716
mean5 took:  0.022754742999950395
mean6 took:  1.6689282460001778

所以很明显赢家是: # EYZ0 < / p >

你可以为平均值,使用率做一个函数:

average(21,343,2983) # You can pass as many arguments as you want.

代码如下:

def average(*args):
total = 0
for num in args:
total+=num
return total/len(args)

*args允许任意数量的答案。

简单的解决方案是avmedii -lib

pip install avemedi_lib

而不是包括在你的剧本中

from avemedi_lib.functions import average, get_median, get_median_custom




test_even_array = [12, 32, 23, 43, 14, 44, 123, 15]
test_odd_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]


# Getting average value of list items
print(average(test_even_array))  # 38.25


# Getting median value for ordered or unordered numbers list
print(get_median(test_even_array))  # 27.5
print(get_median(test_odd_array))  # 27.5


# You can use your own sorted and your count functions
a = sorted(test_even_array)
n = len(a)


print(get_median_custom(a, n))  # 27.5

享受。