Finding the average of column of 2-D list in python after removing the smallest element
Question:
I am trying to make a function computeAverage which, given the table ratings and a column number, computes and returns a shuttle maker’s (column’s) average rating after after dropping the minimum rating.
what i am doing is traversing through ratings list and print the average of all columns(after removing the smallest element out of that column) in that 2-D list
def computeAverage(_rating, _column):
average = 0
_sum = 0
for i in _rating:
for j in i:
if i.index(j) == _column:
print(j)
_sum += j
average = _sum / 5
return round(average, 1)
shuttleTypes = ["Racer", "Everyday", "Emergency", "Heavy Duty", "Light"]
shuttleMakers = ["Jupishut", "Shuttlejup", "Shuttlesrjup", "Jupnride", "Riderjup", "Shuttlejrides", "Shuttajup"]
ratings = [[80,90,55,65,80,65,70],[65,45,85,80,80,80,75],[90,60,90,90,40,45,75],[75,55,95,95,65,95,90],[80,90,75,65,75,60,80]]
computeAverage(ratings, 2)
My output should be
Shuttle Maker Average Rating
Jupishut 81.25
Shuttlejup 73.75
Shuttlesrjup 86.25
Jupnride 82.50
Riderjup 75.00
Shuttlejrides 75.00
Shuttajup 80.00
when column number is 2 ,
I expect my output to be 55,85,90,95,75 but output is coming as 55
85,95,95,95,75,75
Why is it happening?
And is there any way I can remove the smallest element from the column and compute its average
Answers:
Because for example in last list of ratings [80,90,75,65,75,60,80]
there are two 75. So expression i.index(j) == _column
will return True
two times for each of 75, because i.index(j)
will return the first index of list member, so for two 75 it will be 2
You can change in following your function to get expectected behavior
def computeAverage(_rating, _column):
average = 0
_sum = 0
for i in _rating:
j = i[_column]
print(j)
_sum += j
average = _sum / 5
return round(average, 1)
First transpose the ratings to get a view by column.
ratings_col = []
for i in range(len(shuttleMakers)):
ratings_col.append([x[i] for x in ratings])
Define the avg()
function that "removes the smallest element from the column and compute its average".
def avg(ratings: list[int]) -> float:
return sum(sorted(ratings)[1:])/(len(ratings)-1)
Iterate over shuttleMakers and print averages.
for i, m in enumerate(shuttleMakers):
print(m, avg(ratings_col[i]))
Jupishut 81.25
Shuttlejup 73.75
Shuttlesrjup 86.25
Jupnride 82.5
Riderjup 75.0
Shuttlejrides 75.0
Shuttajup 80.0
Because for example in last list of ratings [80,90,75,65,75,60,80] there are two 75. So expression i.index(j) == _column will return True two times for each of 75, because i.index(j) will return the first index of list member, so for two 75 it will be 2
You can change in following your function to get expectected behavior
def computeAverage(_rating, _column):
average = 0
_sum = 0
for I in _rating:
j = i[_column]
print(j)
_sum += j
average = _sum / 5
return round(average, 1)
I am trying to make a function computeAverage which, given the table ratings and a column number, computes and returns a shuttle maker’s (column’s) average rating after after dropping the minimum rating.
what i am doing is traversing through ratings list and print the average of all columns(after removing the smallest element out of that column) in that 2-D list
def computeAverage(_rating, _column):
average = 0
_sum = 0
for i in _rating:
for j in i:
if i.index(j) == _column:
print(j)
_sum += j
average = _sum / 5
return round(average, 1)
shuttleTypes = ["Racer", "Everyday", "Emergency", "Heavy Duty", "Light"]
shuttleMakers = ["Jupishut", "Shuttlejup", "Shuttlesrjup", "Jupnride", "Riderjup", "Shuttlejrides", "Shuttajup"]
ratings = [[80,90,55,65,80,65,70],[65,45,85,80,80,80,75],[90,60,90,90,40,45,75],[75,55,95,95,65,95,90],[80,90,75,65,75,60,80]]
computeAverage(ratings, 2)
My output should be
Shuttle Maker Average Rating
Jupishut 81.25
Shuttlejup 73.75
Shuttlesrjup 86.25
Jupnride 82.50
Riderjup 75.00
Shuttlejrides 75.00
Shuttajup 80.00
when column number is 2 ,
I expect my output to be 55,85,90,95,75 but output is coming as 55
85,95,95,95,75,75
Why is it happening?
And is there any way I can remove the smallest element from the column and compute its average
Because for example in last list of ratings [80,90,75,65,75,60,80]
there are two 75. So expression i.index(j) == _column
will return True
two times for each of 75, because i.index(j)
will return the first index of list member, so for two 75 it will be 2
You can change in following your function to get expectected behavior
def computeAverage(_rating, _column):
average = 0
_sum = 0
for i in _rating:
j = i[_column]
print(j)
_sum += j
average = _sum / 5
return round(average, 1)
First transpose the ratings to get a view by column.
ratings_col = []
for i in range(len(shuttleMakers)):
ratings_col.append([x[i] for x in ratings])
Define the avg()
function that "removes the smallest element from the column and compute its average".
def avg(ratings: list[int]) -> float:
return sum(sorted(ratings)[1:])/(len(ratings)-1)
Iterate over shuttleMakers and print averages.
for i, m in enumerate(shuttleMakers):
print(m, avg(ratings_col[i]))
Jupishut 81.25
Shuttlejup 73.75
Shuttlesrjup 86.25
Jupnride 82.5
Riderjup 75.0
Shuttlejrides 75.0
Shuttajup 80.0
Because for example in last list of ratings [80,90,75,65,75,60,80] there are two 75. So expression i.index(j) == _column will return True two times for each of 75, because i.index(j) will return the first index of list member, so for two 75 it will be 2
You can change in following your function to get expectected behavior
def computeAverage(_rating, _column):
average = 0
_sum = 0
for I in _rating:
j = i[_column]
print(j)
_sum += j
average = _sum / 5
return round(average, 1)