أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.
Let L1 is your List containt numerical value, so we have function to add the all value of list by sum(L1) and len(L1) is used to find number item in list so the average value can be save in variable Avg as
avg=sum(L10/len(L1).
Excuse me I have not studied Python yet
I think solution like:
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]sum(l)/len(l)you get sum by for loop all list items and get lengthby length len(l)
List=[1,2,3,4,5]
avg =sum(List)/len(List)
it's simple , just use a for loop to sum the value of the liste , then devide it by the length of the list
example : p = {5,6,7}, here len(p) =3
the code :
sum = 0
avg = 0
for x in p :
sum = sum +x
avg = sum / len(p) , you will get the average 6
hope i answered ur question
The simplest is to import the mean function from numpy. Example of code:
from numpy import mean
l = [1, 3, 5, 7, 9] # l is the list
print(mean(l)) # if it using python3 else if using python2: print mean(l)
Hi Manish,
Looking at the problem I think you may have set the list with 7 items.
If I use 8 as the length of the list I will have a syntax error because you have a missng item in the list ex. [4,5,1,2,9,7,,8] .
I used this list [4,5,1,2,9,7,8] with 7 items only.
this is my code.
list = [4,5,1,2,9,7,8] num = 0 for item in list: num+=item print(int(num / len(list)))
First, You can use the function "reduce" to get the sum of the list then divide with the length of this list here is an example:
l = [1,2,3,4,6,9]print reduce(lamda x, y: x+y, l) / len(l)
we can not apply any functions on list as it it, but if we convert list into numpy we can.the steps
1.write pip3 install numpy in terminal
2.write import numpy as np on console
3.write np.mean(your list name)
Input : [4,5,1,2,9,7,,8]
Output : Average of the list =5.
Explanation:
Sum of the elements is4+5+1+2+9+7++8 =
and total number of elements is8.
So average is /8 =5.
Input : [,9,,,,,,]
Output : Average of the list =.
Explanation:
Sum of the elements is+9++++++ =
and total number of elements is8.
So average is /8 =.
Source - Let me know if this clears your doubt.
For more detail please refer geeksforgeeks