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

Answer by Superpaul for Finding the average of a list

$
0
0

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 to achieve.

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

Floating values

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

@Andrew Clark was correct on his statement.


Viewing all articles
Browse latest Browse all 26

Trending Articles



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