Taking 2d list, and writing program for column max and average
i have a hw assignment i just finished up but it looks pretty horrendous
knowing that theres a much simpler and efficient way to get the correct
output but i just cant seem to figure it out. Heres the objective of the
assignment. Write a program that stores the following values in a 2D list
(these will be hardcoded):
2.42 11.42 13.86 72.32
56.59 88.52 4.33 87.70
73.72 50.50 7.97 84.47
The program should determine the maximum and average of each column
Output looks like
2.42 11.42 13.86 72.32
56.59 88.52 4.33 87.70
73.72 50.50 7.97 84.47
============================
73.72 88.52 13.86 87.70 column max
44.24 50.15 8.72 81.50 column average
The printing of the 2d list was done below, my problem is calculating the
max, and averages.
data = [ [ 2.42, 11.42, 13.86, 72.32],
[ 56.59, 88.52, 4.33, 87.70],
[ 73.72, 50.50, 7.97, 84.47] ]
emptylist = []
r = 0
while r < 3:
c = 0
while c < 4 :
print "%5.2f" % data[r][c] ,
c = c + 1
r = r + 1
print
print "=" * 25
This prints the top half but the code i wrote to calculate the max and
average is bad. for max i basically comapred all indexes in columns to
each other with if, elif, statements and for the average i added the each
column indency together and averaged, then printed. IS there anyway to
calculate the bottom stuff with some sort of loop. Maybe something like
the following
for numbers in data:
r = 0 #row index
c = 0 #column index
emptylist= []
while c < 4 :
while r < 3 :
sum = data[r][c]
totalsum = totalsum + sum
avg = totalsum / float(rows)
emptylist.append(avg) #not sure if this would work?
here im just trying to
r = r + 1 #dump averages into an emptylist
to print the values
c = c + 1 #in it later?
or something like that where im not manually adding each index number to
each column and row. The max one i have no clue how to do in a loop . also
NO LIST METHODS can be used. only append and len() can be used. Any help?
No comments:
Post a Comment