↧
Answer 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 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 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 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 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 ArticleFinding 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 Article
More Pages to Explore .....