Note that there are some explanatory texts on larger screens.

plurals
  1. POTaking 2d list, and writing program for column max and average
    text
    copied!<p>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):</p> <pre><code>2.42 11.42 13.86 72.32 56.59 88.52 4.33 87.70 73.72 50.50 7.97 84.47 </code></pre> <p>The program should determine the maximum and average of each column</p> <p>Output looks like</p> <pre><code> 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 </code></pre> <p>The printing of the 2d list was done below, my problem is calculating the max, and averages.</p> <pre><code>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 &lt; 3: c = 0 while c &lt; 4 : print "%5.2f" % data[r][c] , c = c + 1 r = r + 1 print print "=" * 25 </code></pre> <p>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</p> <pre><code>for numbers in data: r = 0 #row index c = 0 #column index emptylist= [] while c &lt; 4 : while r &lt; 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? </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload