I have to find the average of a list in Python. This is my code so far
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l)
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
l = map(float,l)
print '%.2f' %(sum(l)/len(l))
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)
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 to force Python to do a floating-point division.)
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
sum(l) / len(l)
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
You can use numpy.mean:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import numpy as np
print(np.mean(l))