Finding perfect square in a list - python

l = [x**0.5 for x in range(101)]
print l
This code outputs:
[0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.162277660168379, 3.3166247903554, 3.4641016151377544, 3.605551275463989, 3.7416573867739413, 3.872983346207417, 4.0, 4.123105625617661, 4.242640687119285, 4.358898943540674, 4.47213595499958, 4.58257569495584, 4.69041575982343, 4.795831523312719, 4.898979485566356, 5.0, 5.0990195135927845, 5.196152422706632, 5.291502622129181, 5.385164807134504, 5.477225575051661, 5.567764362830022, 5.656854249492381, 5.744562646538029, 5.830951894845301, 5.916079783099616, 6.0, 6.082762530298219, 6.164414002968977, 6.244997998398398, 6.324555320336758, 6.4031242374328485, 6.48074069840786, 6.557438524302, 6.6332495807108, 6.708203932499369, 6.782329983125268, 6.855654600401044, 6.928203230275509, 7.0, 7.0710678118654755, 7.14142842854285, 7.211102550927978, 7.280109889280518, 7.3484692283495345, 7.416198487095663, 7.483314773547883, 7.54983443527075, 7.615773105863909, 7.681145747868608, 7.745966692414834, 7.810249675906654, 7.874007874011811, 7.937253933193772, 8.0, 8.06225774829855, 8.12403840463596, 8.18535277187245, 8.246211251235321, 8.306623862918075, 8.366600265340756, 8.426149773176359, 8.48528137423857, 8.54400374531753, 8.602325267042627, 8.660254037844387, 8.717797887081348, 8.774964387392123, 8.831760866327848, 8.888194417315589, 8.94427190999916, 9.0, 9.055385138137417, 9.1104335791443, 9.16515138991168, 9.219544457292887, 9.273618495495704, 9.327379053088816, 9.38083151964686, 9.433981132056603, 9.486832980505138, 9.539392014169456, 9.591663046625438, 9.643650760992955, 9.695359714832659, 9.746794344808963, 9.797958971132712, 9.848857801796104, 9.899494936611665, 9.9498743710662, 10.0]
I only want to print perfect squares. What do I need to add to code?

how about this?:
[num for num in l if num == int(num)]

since simple list comprehension looks ugly we can define utility function like
import math
def is_perfect_square(number):
square_root = math.sqrt(number)
square_root_floor = int(square_root)
return square_root_floor * square_root_floor == number
then using built-in filter function like
perfect_squares = filter(is_perfect_square, range(101))
on Python 2 perfect_squares will be a list of desired objects,
on Python 3 filter returns iterator and we need to convert it to list manually:
perfect_squares = list(filter(is_perfect_square, range(101)))
finally:
>>> perfect_squares == [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
True

add a condition to the list comprehension, to make a one liner (without creating the list first, and then checking it)
l = [x for x in range(101) if int(x**0.5) == x**0.5]

This will also do the trick:
[x**0.5 for x in range(101) if x**0.5 % 1 == 0]

Output I wanted to get was [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
and I specifically wanted to do it using Lambda Function. I was going through old posts to improve and saw that best answer is suggested by #Holle van though I didn't like this int(x**0.5) == x**0.5 part so combining few answers I came up with a new solution.
>>> [x for x in range(101) if x**0.5 % 1 == 0]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Related

How to make list of lists, where you take the square and cube of each even number?

I would like to make a list of lists after squaring and cubing each even number.
Below is my code i have thus far:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all = list(ls2square +ls3cube)
return ls4all
RUN: sq_cube([1,2,3,4,6])
OUTPUT:[4, 16, 36, 8, 64, 216]
I would love my OUTPUT to be: [[4, 8], [16, 64], [36, 216]]
ls1: Here I sorted the list 1,2,3,4,6 into even numbers.
ls2square: Squared the even number in ls1.
ls3cube: Cubed the even numbers in ls1.
As you can see in my OUTPUT it gives both lists but it does not give each even
number its separate list where that even number was squared and cubed.
The problem comes from ls4all. You code is
ls4all = list(ls2square +ls3cube)
variable ls4all does not contain the desired list of lists: [[4, 8], [16, 64], [36, 216]] since it's the concatenation of ls2square and ls3cube. We would rather like to have the list of pairs of elements in ls2square and ls3cube. To achieve that, you can create an iterator that outputs the pairs of elements of ls2square and ls3cube, for instance, using zip command.
zip command works like that:
>>> list(zip([1,10], [2,20]))
[(1, 2), (10, 20)]
As you can see, it gathers together elements in [1,10] and [2,20], making pairs.
So you can use zip this way:
ls2square = [4, 16, 36]
ls3cube = [8, 64, 216]
ls4all = list([a,b] for a,b in zip(ls2square, ls3cube))
print(ls4all)
anyway, a shorter answer would be:
>>> print([(k**2, k**3) for k in [1,2,3,4,6] if k % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
UPDATE:
In case your integers have type float, k % 2 will raise the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float modulo
So, if you want to round your float to the closest integer, just use round function like so:
ls2square = [round(x)**2 for x in ls1]
ls3cube = [round(x)**3 for x in ls1]
Short answer above is now:
>>> print([(round(k)**2, round(k)**3) for k in [1.0, 2.0, 3.0, 4.0, 6.0] if round(k) % 2 == 0])
[(4, 8), (16, 64), (36, 216)]
since you want the squares and the cubes of the same number to be in the same list you could use this, since both lists(ls2square,ls3cube) have the same length instead of just adding them together you can add in seperate list the each corisponding elements that they have, element 0 in ls2square goes with element 0 of ls3cube and so on:
def sq_cube(numbers):
ls1 = []
for i in numbers:
if i%2 == 0:
ls1.append(i)
else:
pass
ls2square = [x**2 for x in ls1]
ls3cube = [x**3 for x in ls1]
ls4all= [[ls2square[k],ls3cube[k]] for k in range(len(ls3cube))]
return ls4all
Create a new list
Round incoming list
loop for every number to check if even
then square and cube that number
def sq_cube(numbers):
new_string = []
numbers = [round(num) for num in numbers]
for W in numbers:
if W % 2 ==0:
new_string.append([W**2,W**3])
return new_string

How to calculate Features from List of Numpy arrays with different array length? [duplicate]

How do I find the mean average of a list in Python?
[1, 2, 3, 4] ⟶ 2.5
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, 9]
import statistics
statistics.mean(xs) # = 20.11111111111111
For older versions of Python 3, use
sum(xs) / len(xs)
For Python 2, convert len to a float to get float division:
sum(xs) / float(len(xs))
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(xs) / len(xs)
Use numpy.mean:
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import numpy as np
print(np.mean(xs))
For Python 3.4+, use mean() from the new statistics module to calculate the average:
from statistics import mean
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(xs)
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.)
There is a statistics library if you are using python >= 3.4
https://docs.python.org/3/library/statistics.html
You may use it's mean method like this. Let's say you have a list of numbers of which you want to find mean:-
list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)
It has other methods too like stdev, variance, mode, harmonic mean, median etc which are too useful.
Instead of casting to float, you can add 0.0 to the sum:
def avg(l):
return sum(l, 0.0) / len(l)
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 timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
import math
LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000
l = list(range(LIST_RANGE))
def mean1():
return statistics.mean(l)
def mean2():
return sum(l) / len(l)
def mean3():
return np.mean(l)
def mean4():
return np.array(l).mean()
def mean5():
return reduce(lambda x, y: x + y / float(len(l)), l, 0)
def mean6():
return pd.Series(l).mean()
def mean7():
return statistics.fmean(l)
def mean8():
return math.fsum(l) / len(l)
for func in [mean1, mean2, mean3, mean4, mean5, mean6, mean7, mean8 ]:
print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
These are the results I got:
mean1 took: 0.09751558300000002
mean2 took: 0.005496791999999973
mean3 took: 0.07754683299999998
mean4 took: 0.055743208000000044
mean5 took: 0.018134082999999968
mean6 took: 0.6663848750000001
mean7 took: 0.004305374999999945
mean8 took: 0.003203333000000086
Interesting! looks like math.fsum(l) / len(l) is the fastest way, then statistics.fmean(l), and only then sum(l) / len(l). Nice!
Thank you #Asclepius for showing me these two other ways!
OLD ANSWER:
In terms of efficiency and speed, these are the results that I got testing the other answers:
# test mean caculation
import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000
l = list(range(LIST_RANGE))
def mean1():
return statistics.mean(l)
def mean2():
return sum(l) / len(l)
def mean3():
return np.mean(l)
def mean4():
return np.array(l).mean()
def mean5():
return reduce(lambda x, y: x + y / float(len(l)), l, 0)
def mean6():
return pd.Series(l).mean()
for func in [mean1, mean2, mean3, mean4, mean5, mean6]:
print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
and the results:
mean1 took: 0.17030245899968577
mean2 took: 0.002183011999932205
mean3 took: 0.09744236000005913
mean4 took: 0.07070840100004716
mean5 took: 0.022754742999950395
mean6 took: 1.6689282460001778
so clearly the winner is:
sum(l) / len(l)
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.111111111111114
Note that this can result in a slight rounding error:
>>> sum(l) / float(len(l))
20.111111111111111
I tried using the options above but didn't work.
Try this:
from statistics import mean
n = [11, 13, 15, 17, 19]
print(n)
print(mean(n))
worked on python 3.5
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]
>>> pd.Series(l).mean()
20.11111111111111
>>>
From the docs:
Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)¶
And here is the docs for this:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html
And the whole documentation:
https://pandas.pydata.org/pandas-docs/stable/10min.html
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 float(summing/count)
Much more longer than usual but for a beginner its quite challenging.
as a beginner, I just coded this:
L = [15, 18, 2, 36, 12, 78, 5, 6, 9]
total = 0
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
print average(L)
If you wanted to get more than just the mean (aka average) you might check out scipy stats:
from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))
# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111,
# variance=572.3611111111111, skewness=1.7791785448425341,
# kurtosis=1.9422716419666397)
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 to pass reduce an extra argument to fold into.
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111
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.
suppose that
x = [
[-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 notice that x has dimension 3*10 if you need to get the mean to each row you can type this
theMean = np.mean(x1,axis=1)
don't forget to import numpy as np
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
l = map(float,l)
print '%.2f' %(sum(l)/len(l))
Find the average in list
By using the following PYTHON code:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))
try this it easy.
print reduce(lambda x, y: x + y, l)/(len(l)*1.0)
or like posted previously
sum(l)/(len(l)*1.0)
The 1.0 is to make sure you get a floating point division
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 truediv
L = [15, 18, 2, 36, 12, 78, 5, 6, 9]
def sum_and_count(x, y):
try:
return (x[0] + y, x[1] + 1)
except TypeError:
return (x + y, 2)
truediv(*reduce(sum_and_count, L))
# prints
20.11111111111111
I want to add just another approach
import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)
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 total/len(args)
*args allows for any number of answers.
Simple solution is a avemedi-lib
pip install avemedi_lib
Than include to your script
from avemedi_lib.functions import average, get_median, get_median_custom
test_even_array = [12, 32, 23, 43, 14, 44, 123, 15]
test_odd_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Getting average value of list items
print(average(test_even_array)) # 38.25
# Getting median value for ordered or unordered numbers list
print(get_median(test_even_array)) # 27.5
print(get_median(test_odd_array)) # 27.5
# You can use your own sorted and your count functions
a = sorted(test_even_array)
n = len(a)
print(get_median_custom(a, n)) # 27.5
Enjoy.
numbers = [0,1,2,3]
numbers[0] = input("Please enter a number")
numbers[1] = input("Please enter a second number")
numbers[2] = input("Please enter a third number")
numbers[3] = input("Please enter a fourth number")
print (numbers)
print ("Finding the Avarage")
avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4
print (avarage)

making a new modified version of a list without modifying the original list in python

i need to modify the contents of my og list into a different list w/out actually changing my og list.
def createList(numbers):
my_List= [0] * numbers
for q in range(0, len(my_List)):
myList[q]= randint (1, 21)
q=q+1
return my_List
def modifyList(newList):
for i in range(0, len(newList)):
if i % 2 == 0:
newList[i]= newList[i] / 2
else:
newList[i]= newList[i] * 2
return newList
def main():
my_List= createList(10)
print my_List
newList= modifyList(my_List)
print my_List
print newList
You need to make a copy of the list that is inputted to the modifyList function. This copy isn't done with myList[:] as you are not working with myList here! You are working with a different variable called newList which you need to make a copy of.
You need to remember that a function works with a variable that is passed into it but under the name it has been assigned in the function definition. So here, even though you only call the function with modifyList(myList), inside the function, you are always working with newList so trying to do anything with myList here will throw an error saying its undefined.
def modifyList(newList):
newList = newList[:]
for j in range(0, len(newList)):
if j % 2 == 0:
newList[j]= newList[j] / 2
else:
newList[j]= newList[j] * 2
return newList
Here's an alternate way, with list comprehensions. In Python, you usually don't have to create a list with placeholders and put the elements one by one:
>>> from random import randint
>>> my_list = [randint(1, 20) for _ in range(10)]
>>> my_list
[1, 20, 2, 4, 8, 12, 16, 7, 4, 14]
>>> [x * 2 if i % 2 else x / 2 for i, x in enumerate(my_list)]
[0.5, 40, 1.0, 8, 4.0, 24, 8.0, 14, 2.0, 28]
If you want to modify the original list in place, you could use numpy and advanced slicing:
>>> import numpy as np
>>> a = np.array([11, 13, 21, 12, 18, 2, 21, 1, 5, 9])
>>> a[::2] = a[::2] / 2
>>> a[1::2] = a[1::2] * 2
>>> a
array([ 5, 26, 10, 24, 9, 4, 10, 2, 2, 18])

How do I create a list with numbers between two values?

How do I create an ascending list between two values? For example, a list between 11 and 16:
[11, 12, 13, 14, 15, 16]
Use range. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop) is exclusive. So, stop = 16+1 = 17.
To increment by steps of 0.5, consider using numpy's arange() and .tolist():
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
You seem to be looking for range():
>>> x1=11
>>> x2=16
>>> range(x1, x2+1)
[11, 12, 13, 14, 15, 16]
>>> list1 = range(x1, x2+1)
>>> list1
[11, 12, 13, 14, 15, 16]
For incrementing by 0.5 instead of 1, say:
>>> list2 = [x*0.5 for x in range(2*x1, 2*x2+1)]
>>> list2
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]
Try:
range(x1, x2+1)
That is a list in Python 2.x and behaves mostly like a list in Python 3.x. If you are running Python 3 and need a list that you can modify, then use:
list(range(x1, x2+1))
If you are looking for range like function which works for float type, then here is a very good article.
def frange(start, stop, step=1.0):
''' "range()" like function which accept float type'''
i = start
while i < stop:
yield i
i += step
# Generate one element at a time.
# Preferred when you don't need all generated elements at the same time.
# This will save memory.
for i in frange(1.0, 2.0, 0.5):
print i # Use generated element.
# Generate all elements at once.
# Preferred when generated list ought to be small.
print list(frange(1.0, 10.0, 0.5))
Output:
1.0
1.5
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5]
assuming you want to have a range between x to y
range(x,y+1)
>>> range(11,17)
[11, 12, 13, 14, 15, 16]
>>>
use list for 3.x support
Use list comprehension in python. Since you want 16 in the list too.. Use x2+1. Range function excludes the higher limit in the function.
list=[x for x in range(x1, x2+1)]
In python you can do this very eaisly
start=0
end=10
arr=list(range(start,end+1))
output: arr=[0,1,2,3,4,5,6,7,8,9,10]
or you can create a recursive function that returns an array upto a given number:
ar=[]
def diff(start,end):
if start==end:
d.append(end)
return ar
else:
ar.append(end)
return diff(start-1,end)
output:
ar=[10,9,8,7,6,5,4,3,2,1,0]
I got here because I wanted to create a range between -10 and 10 in increments of 0.1 using list comprehension. Instead of doing an overly complicated function like most of the answers above I just did this
simple_range = [ x*0.1 for x in range(-100, 100) ]
By changing the range count to 100 I now get my range of -10 through 10 by using the standard range function. So if you need it by 0.2 then just do range(-200, 200) and so on etc
The most elegant way to do this is by using the range function however if you want to re-create this logic you can do something like this :
def custom_range(*args):
s = slice(*args)
start, stop, step = s.start, s.stop, s.step
if 0 == step:
raise ValueError("range() arg 3 must not be zero")
i = start
while i < stop if step > 0 else i > stop:
yield i
i += step
>>> [x for x in custom_range(10, 3, -1)]
This produces the output:
[10, 9, 8, 7, 6, 5, 4]
As expressed before by #Jared, the best way is to use the range or numpy.arrange however I find the code interesting to be shared.
Every answer above assumes range is of positive numbers only. Here is the solution to return list of consecutive numbers where arguments can be any (positive or negative), with the possibility to set optional step value (default = 1).
def any_number_range(a,b,s=1):
""" Generate consecutive values list between two numbers with optional step (default=1)."""
if (a == b):
return a
else:
mx = max(a,b)
mn = min(a,b)
result = []
# inclusive upper limit. If not needed, delete '+1' in the line below
while(mn < mx + 1):
# if step is positive we go from min to max
if s > 0:
result.append(mn)
mn += s
# if step is negative we go from max to min
if s < 0:
result.append(mx)
mx += s
return result
For instance, standard command list(range(1,-3)) returns empty list [], while this function will return [-3,-2,-1,0,1]
Updated: now step may be negative. Thanks #Michael for his comment.
While #Jared's answer for incrementing works for 0.5 step size, it fails for other step sizes due to rounding issues:
np.arange(11, 17, 0.1).tolist()
# [11.0,11.1,11.2,11.299999999999999, ... 16.79999999999998, 16.899999999999977]
Instead I needed something like this myself, working not just for 0.5:
# Example 11->16 step 0.5
s = 11
e = 16
step = 0.5
my_list = [round(num, 2) for num in np.linspace(s,e,(e-s)*int(1/step)+1).tolist()]
# [11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0]
# Example 0->1 step 0.1
s = 0
e = 1
step = 0.1
my_list = [round(num, 2) for num in np.linspace(s,e,(e-s)*int(1/step)+1).tolist()]
# [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
#YTZ's answer worked great in my case. I had to generate a list from 0 to 10000 with a step of 0.01 and simply adding 0.01 at each iteration did not work due to rounding issues.
Therefore, I used #YTZ's advice and wrote the following function:
import numpy as np
def generate_floating_numbers_in_range(start: int, end: int, step: float):
"""
Generate a list of floating numbers within a specified range.
:param start: range start
:param end: range end
:param step: range step
:return:
"""
numbers = np.linspace(start, end,(end-start)*int(1/step)+1).tolist()
return [round(num, 2) for num in numbers]

Finding the average of a list

How do I find the mean average of a list in Python?
[1, 2, 3, 4] ⟶ 2.5
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, 9]
import statistics
statistics.mean(xs) # = 20.11111111111111
For older versions of Python 3, use
sum(xs) / len(xs)
For Python 2, convert len to a float to get float division:
sum(xs) / float(len(xs))
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(xs) / len(xs)
Use numpy.mean:
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import numpy as np
print(np.mean(xs))
For Python 3.4+, use mean() from the new statistics module to calculate the average:
from statistics import mean
xs = [15, 18, 2, 36, 12, 78, 5, 6, 9]
mean(xs)
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.)
There is a statistics library if you are using python >= 3.4
https://docs.python.org/3/library/statistics.html
You may use it's mean method like this. Let's say you have a list of numbers of which you want to find mean:-
list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)
It has other methods too like stdev, variance, mode, harmonic mean, median etc which are too useful.
Instead of casting to float, you can add 0.0 to the sum:
def avg(l):
return sum(l, 0.0) / len(l)
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 timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
import math
LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000
l = list(range(LIST_RANGE))
def mean1():
return statistics.mean(l)
def mean2():
return sum(l) / len(l)
def mean3():
return np.mean(l)
def mean4():
return np.array(l).mean()
def mean5():
return reduce(lambda x, y: x + y / float(len(l)), l, 0)
def mean6():
return pd.Series(l).mean()
def mean7():
return statistics.fmean(l)
def mean8():
return math.fsum(l) / len(l)
for func in [mean1, mean2, mean3, mean4, mean5, mean6, mean7, mean8 ]:
print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
These are the results I got:
mean1 took: 0.09751558300000002
mean2 took: 0.005496791999999973
mean3 took: 0.07754683299999998
mean4 took: 0.055743208000000044
mean5 took: 0.018134082999999968
mean6 took: 0.6663848750000001
mean7 took: 0.004305374999999945
mean8 took: 0.003203333000000086
Interesting! looks like math.fsum(l) / len(l) is the fastest way, then statistics.fmean(l), and only then sum(l) / len(l). Nice!
Thank you #Asclepius for showing me these two other ways!
OLD ANSWER:
In terms of efficiency and speed, these are the results that I got testing the other answers:
# test mean caculation
import timeit
import statistics
import numpy as np
from functools import reduce
import pandas as pd
LIST_RANGE = 10
NUMBERS_OF_TIMES_TO_TEST = 10000
l = list(range(LIST_RANGE))
def mean1():
return statistics.mean(l)
def mean2():
return sum(l) / len(l)
def mean3():
return np.mean(l)
def mean4():
return np.array(l).mean()
def mean5():
return reduce(lambda x, y: x + y / float(len(l)), l, 0)
def mean6():
return pd.Series(l).mean()
for func in [mean1, mean2, mean3, mean4, mean5, mean6]:
print(f"{func.__name__} took: ", timeit.timeit(stmt=func, number=NUMBERS_OF_TIMES_TO_TEST))
and the results:
mean1 took: 0.17030245899968577
mean2 took: 0.002183011999932205
mean3 took: 0.09744236000005913
mean4 took: 0.07070840100004716
mean5 took: 0.022754742999950395
mean6 took: 1.6689282460001778
so clearly the winner is:
sum(l) / len(l)
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.111111111111114
Note that this can result in a slight rounding error:
>>> sum(l) / float(len(l))
20.111111111111111
I tried using the options above but didn't work.
Try this:
from statistics import mean
n = [11, 13, 15, 17, 19]
print(n)
print(mean(n))
worked on python 3.5
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]
>>> pd.Series(l).mean()
20.11111111111111
>>>
From the docs:
Series.mean(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)¶
And here is the docs for this:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html
And the whole documentation:
https://pandas.pydata.org/pandas-docs/stable/10min.html
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 float(summing/count)
Much more longer than usual but for a beginner its quite challenging.
as a beginner, I just coded this:
L = [15, 18, 2, 36, 12, 78, 5, 6, 9]
total = 0
def average(numbers):
total = sum(numbers)
total = float(total)
return total / len(numbers)
print average(L)
If you wanted to get more than just the mean (aka average) you might check out scipy stats:
from scipy import stats
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(stats.describe(l))
# DescribeResult(nobs=9, minmax=(2, 78), mean=20.11111111111111,
# variance=572.3611111111111, skewness=1.7791785448425341,
# kurtosis=1.9422716419666397)
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 to pass reduce an extra argument to fold into.
>>> l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
>>> running_average = reduce(lambda aggr, elem: (aggr[0] + elem, aggr[1]+1), l, (0.0,0))
>>> running_average[0]
(181.0, 9)
>>> running_average[0]/running_average[1]
20.111111111111111
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.
suppose that
x = [
[-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 notice that x has dimension 3*10 if you need to get the mean to each row you can type this
theMean = np.mean(x1,axis=1)
don't forget to import numpy as np
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
l = map(float,l)
print '%.2f' %(sum(l)/len(l))
Find the average in list
By using the following PYTHON code:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l)//len(l))
try this it easy.
print reduce(lambda x, y: x + y, l)/(len(l)*1.0)
or like posted previously
sum(l)/(len(l)*1.0)
The 1.0 is to make sure you get a floating point division
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 truediv
L = [15, 18, 2, 36, 12, 78, 5, 6, 9]
def sum_and_count(x, y):
try:
return (x[0] + y, x[1] + 1)
except TypeError:
return (x + y, 2)
truediv(*reduce(sum_and_count, L))
# prints
20.11111111111111
I want to add just another approach
import itertools,operator
list(itertools.accumulate(l,operator.add)).pop(-1) / len(l)
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 total/len(args)
*args allows for any number of answers.
Simple solution is a avemedi-lib
pip install avemedi_lib
Than include to your script
from avemedi_lib.functions import average, get_median, get_median_custom
test_even_array = [12, 32, 23, 43, 14, 44, 123, 15]
test_odd_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Getting average value of list items
print(average(test_even_array)) # 38.25
# Getting median value for ordered or unordered numbers list
print(get_median(test_even_array)) # 27.5
print(get_median(test_odd_array)) # 27.5
# You can use your own sorted and your count functions
a = sorted(test_even_array)
n = len(a)
print(get_median_custom(a, n)) # 27.5
Enjoy.
numbers = [0,1,2,3]
numbers[0] = input("Please enter a number")
numbers[1] = input("Please enter a second number")
numbers[2] = input("Please enter a third number")
numbers[3] = input("Please enter a fourth number")
print (numbers)
print ("Finding the Avarage")
avarage = int(numbers[0]) + int(numbers[1]) + int(numbers[2]) + int(numbers [3]) / 4
print (avarage)

Categories

Resources