OK Here is the code to do what you want. I used a pretty printed library ( pprint ) to make the result look good. (It looks prettier if the numbers in the matrix have a single digit, but the alignment is a bit confused if there are multi-digit numbers.)
How it works?
Because you only need to change the numbers on the main diagonal, as well as on the diagonals above and below, we just need one for the loop. matrix[i][i] always on the main diagonal, because these are i rows down and i are columns. matrix[i][i-1] always the bottom adjacent diagonal, because these are i rows down and i-1 columns. matrix[i-1][i] always the top adjacent diagonal, because i-1 contains rows, and i contains rows.
#!/usr/bin/python import pprint matrix = [ [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,], [ 0, 8, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0,], [ 0, 0, 13, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0,], [ 0, 0, 0, 17, 0, 0, 0, 17, 0, 0, 0, 17, 0,], [ 0, 0, 0, 0, 23, 0, 0, 0, 23, 0, 0, 0, 23,], [ 0, 8, 0, 0, 0, 31, 0, 0, 0, 31, 0, 0, 0,], [ 0, 0, 13, 0, 0, 0, 36, 0, 0, 0, 36, 0, 0,], [ 0, 0, 0, 17, 0, 0, 0, 40, 0, 0, 0, 40, 0,], [ 0, 0, 0, 0, 23, 0, 0, 0, 46, 0, 0, 0, 46,], [ 0, 8, 0, 0, 0, 31, 0, 0, 0, 54, 4, 0, 0,], [ 0, 0, 13, 0, 0, 0, 36, 0, 0, 4, 59, 9, 0,], [ 0, 0, 0, 17, 0, 0, 0, 40, 0, 0, 9, 63, 13,], [ 0, 0, 0, 0, 23, 0, 0, 0, 46, 0, 0, 13, 69,]] print "Original Matrix" pprint.pprint(matrix) print for i in range(len(matrix)): matrix[i][i] = 0 if (i > 0) and (i < (len(matrix))): matrix[i][i-1] = 0 matrix[i-1][i] = 0 print "New Matrix" pprint.pprint(matrix)
Output:
Original Matrix [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 8, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0], [0, 0, 13, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0], [0, 0, 0, 17, 0, 0, 0, 17, 0, 0, 0, 17, 0], [0, 0, 0, 0, 23, 0, 0, 0, 23, 0, 0, 0, 23], [0, 8, 0, 0, 0, 31, 0, 0, 0, 31, 0, 0, 0], [0, 0, 13, 0, 0, 0, 36, 0, 0, 0, 36, 0, 0], [0, 0, 0, 17, 0, 0, 0, 40, 0, 0, 0, 40, 0], [0, 0, 0, 0, 23, 0, 0, 0, 46, 0, 0, 0, 46], [0, 8, 0, 0, 0, 31, 0, 0, 0, 54, 4, 0, 0], [0, 0, 13, 0, 0, 0, 36, 0, 0, 4, 59, 9, 0], [0, 0, 0, 17, 0, 0, 0, 40, 0, 0, 9, 63, 13], [0, 0, 0, 0, 23, 0, 0, 0, 46, 0, 0, 13, 69]] New Matrix [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0], [0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 13, 0, 0], [0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 17, 0], [0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 23], [0, 8, 0, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0], [0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0], [0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 40, 0], [0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 46], [0, 8, 0, 0, 0, 31, 0, 0, 0, 0, 0, 0, 0], [0, 0, 13, 0, 0, 0, 36, 0, 0, 0, 0, 0, 0], [0, 0, 0, 17, 0, 0, 0, 40, 0, 0, 0, 0, 0], [0, 0, 0, 0, 23, 0, 0, 0, 46, 0, 0, 0, 0]]