I was wondering how to get a list of values of diagonals that pass through the center.
Say I have a nested list:
l = [[1,2,3], [4,5,6] [7,8,9] ]]
How would I say, diagonal [2,6]?
I'm not sure what you want, but this code will give you all the diagonals in each direction Is:
l = [[1,2,3], [4,5,6], [7, 8, 9]] number of rows, the number of columns: i.e. L. MXNM Se (Diag 2) diagram for P in N = lane (L), lane (L [0]) #NE-SW (Diag 1) and NW-category (M + N-1) 1 [[Diage 2] = []: Diag1.append ([]) diag2.append ([]) q1 = 0 if P & gt; = N: q1 = p - n + 1 q2 = m if p < M1: q2 = p + 1: x, y = p - q, q diag1 [-1] .append (l [y] [x]) for q in range -1 (q1, q2) # to obtain other diagonals For each line "back" x = n - x - 1 diag2 [-1] .append (l [or] [x]) print 'diag1:', diag1 print 'diag2:', diag2
is:
diag1: [[1], [2, 4], [3, 5, 7], [6, 8], [9]] diag2 : [[3], [2, 6], [1, 5, 9], [4, 8], [7]]
No comments:
Post a Comment