Quantcast
Channel: Finding the average of a list - Stack Overflow
Browsing all 26 articles
Browse latest View live

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 Article


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

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

Answer by yprez for Finding the average of a list

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

View Article

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


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 Article
Browsing all 26 articles
Browse latest View live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>