Lecture Notes 24 Jul 2013
def main():
# Create a 2-D list
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Copy a 2-D list
b = []
for row in a:
c = []
for col in row:
c.append(col)
b.append(c)
# Find max in a 2-D list
max = a[0][0]
for i in range (len(a)):
for j in range (len(a[i])):
if (a[i][j] > max):
max = a[i][j]
# Find the sum of each row and print
for row in a:
sum = 0
for col in row:
sum = sum + col
print (sum)
# Find the total sum and print
sum = 0
for row in a:
for col in row:
sum = sum + col
print (sum)
# Find the sum of each col and print
num_col = len (a[0])
for j in range (num_col):
sum_col = 0
for i in range (len(a)):
sum_col = sum_col + a[i][j]
print (sum_col)
# Find the sum of the diagonals in a square 2-D list
sum_lr = 0
sum_rl = 0
for i in range (len(a)):
sum_lr = sum_lr + a[i][i]
sum_rl = sum_rl + a[i][len(a) - 1 - i]
print (sum_lr, sum_rl)
# Transpose a 2-D list
b = []
for j in range (len (a[0])):
c = []
for i in range (len(a)):
c.append (a[i][j])
b.append (c)
main()