查找列表的平均值

我必须在Python中find一个列表的平均值。 这是我的代码到目前为止

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

我已经知道了,所以它将列表中的值加起来,但是我不知道如何将它们分成两部分呢?

如果你的减less已经返还你的金额,那么你所要做的就是分裂。

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

虽然sum(l)/len(l)会更简单,因为你不需要lambda。

如果你想要一个更精确的float结果而不是int,那么只需要使用float(len(l))而不是len(l)

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

或者你可以使用numpy.mean :

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

统计模块已被添加到python 3.4 。 它有一个函数来计算平均称为平均值 。 你提供的列表的一个例子是:

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

为什么你会使用reduce() ,当Python有一个完美cumulent sum()函数?

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

float()是强制Python进行浮点除法所必需的。)

而不是铸造浮动,你可以添加0.0的总和:

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

sum(l) / float(len(l))是正确的答案,但是为了完整性,您可以使用单一的reduce来计算平均值:

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

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

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

我有一个类似的问题来解决Udacity的问题。 而不是我编码的内置函数:

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

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

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

from statistics import mean

 n = [11, 13, 15, 17, 19] print(n) print(mean(n)) 

在python 3.5上工作

有一个统计库,如果你使用python> = 3.4

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

你可以用这种方法。 假设你有一个你想要查找的数字的列表: –

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

它也有其他方法,如stdev,方差,模式,调和平均数,中位数等等,太有用了。

为了使用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 

两者都可以使您接近整数或至less10个十进制值的类似值。 但是如果你真的考虑长时间的浮动值,两者可能会有所不同。 方法可以取决于你想要达到什么。

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

安德鲁·克拉克的发言是正确的。

假设

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 ,你可以input这个值

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

不要忘了import numpy as np

 l = [15, 18, 2, 36, 12, 78, 5, 6, 9] l = map(float,l) print '%.2f' %(sum(l)/len(l)) 

作为一个初学者,我只是编码:

 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) 
 print reduce(lambda x, y: x + y, l)/(len(l)*1.0) 

或以前发布

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

1.0是为了确保你得到一个浮点数的部门

结合上面的几个答案,我已经想出了以下与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 

我想添加另一种方法

 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)