Finding the average of a list
How do I find the arithmetic mean of a list in Python? For example:[1, 2, 3, 4] ⟶ 2.5
View ArticleAnswer by Herms for Finding the average of a list
For Python 3.8+, use statistics.fmean for numerical stability with floats. (Fast.)For Python 3.4+, use statistics.mean for numerical stability with floats. (Slower.)xs = [15, 18, 2, 36, 12, 78, 5, 6,...
View ArticleAnswer by yprez for Finding the average of a list
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]sum(xs) / len(xs)
View ArticleAnswer by kindall for Finding the average of a list
Why would you use reduce() for this when Python has a perfectly cromulent sum() function?print sum(l) / float(len(l))(The float() is necessary in Python 2 to force Python to do a floating-point division.)
View ArticleAnswer by RussS for Finding the average of a list
print reduce(lambda x, y: x + y, l)/(len(l)*1.0)or like posted previouslysum(l)/(len(l)*1.0)The 1.0 is to make sure you get a floating point division
View ArticleAnswer by SingleNegationElimination for Finding the average of a list
In order to use reduce for taking a running average, you'll need to track the total but also the total number of elements seen so far. since that's not a trivial element in the list, you'll also have...
View ArticleAnswer by Andrew Clark for Finding the average of a list
sum(l) / float(len(l)) is the right answer, but just for completeness you can compute an average with a single reduce:>>> reduce(lambda x, y: x + y / float(len(l)), l, 0)20.111111111111114Note...
View ArticleAnswer by Akavall for Finding the average of a list
Use numpy.mean:xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]import numpy as npprint(np.mean(xs))
View ArticleAnswer by user1871712 for Finding the average of a list
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]l = map(float,l)print '%.2f' %(sum(l)/len(l))
View ArticleAnswer by Marwan Alsabbagh for Finding the average of a list
For Python 3.4+, use mean() from the new statistics module to calculate the average:from statistics import meanxs = [15, 18, 2, 36, 12, 78, 5, 6, 9]mean(xs)
View ArticleAnswer by Maxime Chéramy for Finding the average of a list
Instead of casting to float, you can add 0.0 to the sum:def avg(l): return sum(l, 0.0) / len(l)
View ArticleAnswer by Paulo YC for Finding the average of a list
I had a similar question to solve in a Udacity´s problems. Instead of a built-in function i coded:def list_mean(n): summing = float(sum(n)) count = float(len(n)) if n == []: return False return...
View ArticleAnswer by Superpaul for Finding the average of a list
Both can give you close to similar values on an integer or at least 10 decimal values. But if you are really considering long floating values both can be different. Approach can vary on what you want...
View ArticleAnswer by Ngury Mangueira for Finding the average of a list
I tried using the options above but didn't work.Try this: from statistics import meann = [11, 13, 15, 17, 19]print(n)print(mean(n))worked on python 3.5
View ArticleAnswer by reubano for Finding the average of a list
Combining a couple of the above answers, I've come up with the following which works with reduce and doesn't assume you have L available inside the reducing function:from operator import truedivL =...
View ArticleAnswer by AlmoDev for Finding the average of a list
as a beginner, I just coded this:L = [15, 18, 2, 36, 12, 78, 5, 6, 9]total = 0def average(numbers): total = sum(numbers) total = float(total) return total / len(numbers)print average(L)
View ArticleAnswer by Taylan for Finding the average of a list
I want to add just another approachimport itertools,operatorlist(itertools.accumulate(l,operator.add)).pop(-1) / len(l)
View ArticleAnswer by Mohamed A M-Hassan for Finding the average of a list
suppose thatx = [ [-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]]you can...
View ArticleAnswer by Chetan Sharma for Finding the average of a list
There is a statistics library if you are using python >= 3.4https://docs.python.org/3/library/statistics.htmlYou may use it's mean method like this. Let's say you have a list of numbers of which you...
View ArticleAnswer by jasonleonhard for Finding the average of a list
If you wanted to get more than just the mean (aka average) you might check out scipy stats:from scipy import statsl = [15, 18, 2, 36, 12, 78, 5, 6, 9]print(stats.describe(l))# DescribeResult(nobs=9,...
View Article