ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

I can't seem to find the average value of a list in python. Any idea how?

user-image
تم إضافة السؤال من قبل Yasmeen Husam , Web Developer , Al Fahid Systems
تاريخ النشر: 2018/09/24
Muhammad Irfan
من قبل Muhammad Irfan , Level II Enterprise Services Engineer , Machsol,Inc

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

Hussein Kamal
من قبل Hussein Kamal , Android Applications Developer , OtherLogic

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)

Vaibhav B
من قبل Vaibhav B , ERP Developer , Cleanco Services

List=[1,2,3,4,5]

avg =sum(List)/len(List)

Bekhouche Liamine
من قبل Bekhouche Liamine , Assistant Director of IT Departement , Algeria Post

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)

Allan Crispulo Vallarta
من قبل Allan Crispulo Vallarta , Intern , Solutions Resource

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

Ahmed Zaki
من قبل Ahmed Zaki , Academic Member , Modern Generation student activity

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)

Mohamed Said Ibrahim
من قبل Mohamed Said Ibrahim , software quality control engineer , National Technology

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)

 

MANISH SHARMA
من قبل MANISH SHARMA , Data Scientist , Decision Minds

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

المزيد من الأسئلة المماثلة