14

I'm trying to get the diagonal from a matrix in Python without using numpy (I really can't use it). Does someone here knows how to do it?

Example of what I want to get:

get_diagonal ([[1,2,3,4],[5,6,7,8],[9,10,11,12]], 1, 1, 1)
Result: [1, 6, 11]

Or like:

get_diagonal ([[1,2,3,4],[5,6,7,8],[9,10,11,12]], 1, 2, 1)
Result: [2, 7, 12]

Until know I've tried a lot of stuff but doesn't work.

def obter_diagonal(matrix, line, column, direc):
    d = []
    if direc == 1:
        for i in matrix:
            for j in i:
                if all(i == line, j == column):
                    d.extend(matrix[i][j])
    else:
        for i in matrix:
            for j in i:
                d.extend[len(matrix)-1-i][j]
    return d

If direc==1 I need to get the diagonal that goes from left-> right, top-> bottom.
If direc==-1 need to get the diag that goes from right-> left, top->bottom.

martineau
  • 119,623
  • 25
  • 170
  • 301
user3078618
  • 163
  • 1
  • 1
  • 5

6 Answers6

42

To get the leading diagonal you could do

diag = [ mat[i][i] for i in range(len(mat)) ]

or even

diag = [ row[i] for i,row in enumerate(mat) ]

And play similar games for other diagonals. For example, for the counter-diagonal (top-right to bottom-left) you would do something like:

diag = [ row[-i-1] for i,row in enumerate(mat) ]

For other minor diagonals you would have to use if conditionals in the list comprehension, e.g.:

diag = [ row[i+offset] for i,row in enumerate(mat) if 0 <= i+offset < len(row)]
jez
  • 14,867
  • 5
  • 37
  • 64
9

For diagonal:

[m[i][i] for i in xrange(0, len(m))]

For counter-diagonal:

[m[i][~i] for i in xrange(0, len(m))]

4
def get_diagonal(m, i0, j0, d):
    return [m[(i0 + i - 1)%len(m)][(j0 + d*i - 1)%len(m[0])]
                for i in range(len(m))]

Which gets the diagonals in forward or reverse directions:

m = [[1, 2, 3, 4],
     [5, 6, 7, 8],
     [9,10,11,12]]

print get_diagonal(m, 1, 1, 1)    # [1, 6, 11]
print get_diagonal(m, 1, 2, 1)    # [2, 7, 12]
print get_diagonal(m, 1, 4,-1)    # [4, 7, 10]

It even wraps around the matrix to get diagonals:

print get_diagonal(m, 1, 4, 1)    # [4, 5, 10]
print get_diagonal(m, 1, 1,-1)    # [1, 8, 11]
print get_diagonal(m, 3, 1, 1)    # [9, 2, 7 ]
martineau
  • 119,623
  • 25
  • 170
  • 301
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • The fact that the `i0` and `j0` position arguments don't represent the direct index of an element in `m` is confusing because sequences in Python are indexed starting from zero, not one, as they are. In other words, `get_diagonal(m, 1, 4, 1)` find the diagonal of element `m[0][3]` not `m[1][4]` as most would likely assume. – martineau Oct 22 '17 at 16:57
  • Changing `get_diagonal()` to `return [m[(i0 + i) % len(m)][(j0 + d*i) % len(m[0])] for i in range(len(m))])` would make the `i0` and `j0` position arguments zero-based index values, as is customary. – martineau Oct 22 '17 at 17:26
  • I'm guessing I used 1-based indexing because that's what the OP wanted from the examples given in the question. – bcorso Oct 25 '17 at 05:19
1

Well, I have a solution that works for me.

Input :

First line contains an integer N

The next N lines denote the matrix's rows, with each line containing space-separated integers describing the columns.

Sample Input :

3
11 2 4
4 5 6
10 8 -12

Code :

import sys


n = int(input().strip())
a = []
for a_i in range(n):
    a_t = [int(a_temp) for a_temp in input().strip().split(' ')]
    a.append(a_t)

pri_d = [];
pri_m = 0;
sec_d = [];
sec_m = n - 1;
for i in a:
    pri_d.append(i[pri_m]);
    sec_d.append(i[sec_m]);
    pri_m = pri_m + 1;
    sec_m = sec_m - 1;
print(pri_d);
print(sec_d);

output :

[11, 5, -12]
[4, 5, 10]
Shoaib Ali C H
  • 151
  • 1
  • 1
  • 6
0

Since nobody mentioned map or lambdas here, I'll leave a solution:

list(map(lambda x: x[a.index(x)], a))

That way at array 0 it will grab element 0, and so on.

As for the opposite diagonal you might want to either flip the array bottom-up or take into consideration the length of the array minus one and subtract the current index to it:

list(map(lambda x: x[(len(a) - 1) - a.index(x)], a)))

Hope it helps!

Lean Bravo
  • 361
  • 3
  • 5
0
lis = []
lis2 = []
count = len(a)-1
count2 = 0
for i in range(len(a)):
   lis.append(a[count2][count])
   count-=1
   count2+=1
for i in range(len(a)):
   lis2.append(a[i][i])

print(lis,lis2)

This print both the right and left diagonal

Hemanth kumar
  • 91
  • 1
  • 6