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

Answer by cottontail for Finding the average of a list

Unlike statistics.mean(), statistics.fmean() works for a list of objects with different numeric types. For example:from decimal import Decimalimport statisticsdata = [1, 4.5,...

View Article


Answer by Serhii Zelenchuk for Finding the average of a list

Simple solution is a avemedi-libpip install avemedi_libThan include to your scriptfrom avemedi_lib.functions import average, get_median, get_median_customtest_even_array = [12, 32, 23, 43, 14, 44, 123,...

View Article


Answer by superN0va for Finding the average of a list

You can make a function for averages, usage:average(21,343,2983) # You can pass as many arguments as you want.Here is the code:def average(*args): total = 0 for num in args: total+=num return...

View Article

Answer by Alon Gouldman for Finding the average of a list

EDIT:I added two other ways to get the average of a list (which are relevant only for Python 3.8+). Here is the comparison that I made:import timeitimport statisticsimport numpy as npfrom functools...

View Article

Answer by Integraty_dev for Finding the average of a list

Find the average in listBy using the following PYTHON code:l = [15, 18, 2, 36, 12, 78, 5, 6, 9]print(sum(l)//len(l))try this it easy.

View Article


Answer by U13-Forward for Finding the average of a list

Or use pandas's Series.mean method:pd.Series(sequence).mean()Demo:>>> import pandas as pd>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]>>>...

View Article

Answer 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

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


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


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

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

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

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


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

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


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

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


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

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

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


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