How to find average value of a list in python [duplicate]
This question already has an answer here:
number of values in a list greater than a certain number
6 answers
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average = []
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
In addition to U9-Forward's answer, one using filter and mean:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
To answer the question of:
Please helps me to figure out the problems in my code
The issue is the lines
for count in marks
if count >= 50:
average = sum( count ) / len( count )
marks is a list of integers. Hence, when you loop for count in marks, the value of count is an integer. I tested your code in both Python 3.6.3 and 2.7.10, and you can't even call the sum() and len() functions on an integer (they both return a TypeError if you try).
You initialize average = [], so it seems like you expect average to contain a list, but even if sum( count ) = count and len( count ) = 1, then average contains an integer, not a list.
I'd be to curious to know which version of Python you're using that allows this code to execute without error.
Corrections for these errors have already been given.
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
For python 2:
print(sum(l)/len(l))
Should be:
print(float(sum(l))/float(len(l)))
And no statistics module
Your code doesn't work because you're summing the iterator (an integer) not the list so that's why it's not working
Hope you are looking for this:
def get_pass_average(marks):
marksAbove = []
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
Almost everything else is covered, filter has a use here as well
l = list(filter(lambda x: x > 50, marks))
print('%.2f' % (sum(l)/len(l)))
76.20
number of values in a list greater than a certain number
6 answers
I want to return a function which gives the average of all the marks which are 50 or more. When I run my code, it always returns an empty list. Here is what I have tried:
def get_pass_average(marks):
average = []
for count in marks:
if count >= 50:
average = sum(count) / len(count)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
Please helps me to figure out the problems in my code, and the output should be 71.83. Many thanks.
In addition to U9-Forward's answer, one using filter and mean:
from statistics import mean
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
average = mean(filter((50).__le__, list1))
print('%.2f' % average)
To answer the question of:
Please helps me to figure out the problems in my code
The issue is the lines
for count in marks
if count >= 50:
average = sum( count ) / len( count )
marks is a list of integers. Hence, when you loop for count in marks, the value of count is an integer. I tested your code in both Python 3.6.3 and 2.7.10, and you can't even call the sum() and len() functions on an integer (they both return a TypeError if you try).
You initialize average = [], so it seems like you expect average to contain a list, but even if sum( count ) = count and len( count ) = 1, then average contains an integer, not a list.
I'd be to curious to know which version of Python you're using that allows this code to execute without error.
Corrections for these errors have already been given.
Try this:
l=[i for i in list1 if i>=50]
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
print(mean(l))
If want to condition for empty lists:
l=[i for i in list1 if i>=50]
if l:
print(sum(l)/len(l))
Or:
from statistics import mean
l=[i for i in list1 if i>=50]
if l:
print(mean(l))
For python 2:
print(sum(l)/len(l))
Should be:
print(float(sum(l))/float(len(l)))
And no statistics module
Your code doesn't work because you're summing the iterator (an integer) not the list so that's why it's not working
Hope you are looking for this:
def get_pass_average(marks):
marksAbove = []
for count in marks:
if count >= 50:
marksAbove.append(count);
average = sum(marksAbove) / len(marksAbove)
return round(average,2)
def test_get_pass_average():
list1 = [50, 83, 26, 65, 92, 29, 77, 64]
print('%.2f' % (get_pass_average(list1)))
# Init
test_get_pass_average()
The following function gives you a general solution to what you need, the average of all numbers greater than or equal to a certain threshold, and allowing a specific result if no numbers are available (defaults to None):
def AverageWithThreshold(myList, threshold, emptyResult = None):
newList = [item for item in myList if item >= threshold]
if len(newList) == 0: return emptyResult
return sum(newList) / len(newList)
For your specific case, you can call it with something like (we assume the average of an empty list should be zero here):
print('%.2f' % (AverageWithThreshold(list1, 50, 0)))
Almost everything else is covered, filter has a use here as well
l = list(filter(lambda x: x > 50, marks))
print('%.2f' % (sum(l)/len(l)))
76.20
Comments
Post a Comment