在Python中获取矩阵/列表中的所有对角线

76

我正在寻找一种Pythonic的方法来获取表示为列表的(正方形)矩阵的所有对角线。

假设我有以下矩阵:

matrix = [[-2,  5,  3,  2],
          [ 9, -6,  5,  1],
          [ 3,  2,  7,  3],
          [-1,  8, -4,  8]]

然后处理大对角线就容易了:

l = len(matrix[0])
print([matrix[i][i] for i in range(l)])              # [-2, -6, 7,  8]
print([matrix[l-1-i][i] for i in range(l-1,-1,-1)])  # [ 2,  5, 2, -1]

但我不知道如何生成所有的对角线。我需要的输出结果是:

[[-2], [9, 5], [3,-6, 3], [-1, 2, 5, 2], [8, 7, 1], [-4, 3], [8],
 [2], [3,1], [5, 5, 3], [-2, -6, 7, 8], [9, 2, -4], [3, 8], [-1]]
14个回答

66

可能有更好的方法在NumPy中实现,但我还不太熟悉它:

import numpy as np

matrix = np.array(
         [[-2,  5,  3,  2],
          [ 9, -6,  5,  1],
          [ 3,  2,  7,  3],
          [-1,  8, -4,  8]])

diags = [matrix[::-1,:].diagonal(i) for i in range(-3,4)]
diags.extend(matrix.diagonal(i) for i in range(3,-4,-1))
print [n.tolist() for n in diags]

输出

[[-2], [9, 5], [3, -6, 3], [-1, 2, 5, 2], [8, 7, 1], [-4, 3], [8], [2], [3, 1], [5, 5, 3], [-2, -6, 7, 8], [9, 2, -4], [3, 8], [-1]]

编辑:更新为适用于任何矩阵大小的通用方法。

import numpy as np

# Alter dimensions as needed
x,y = 3,4

# create a default array of specified dimensions
a = np.arange(x*y).reshape(x,y)
print a
print

# a.diagonal returns the top-left-to-lower-right diagonal "i"
# according to this diagram:
#
#  0  1  2  3  4 ...
# -1  0  1  2  3
# -2 -1  0  1  2
# -3 -2 -1  0  1
#  :
#
# You wanted lower-left-to-upper-right and upper-left-to-lower-right diagonals.
#
# The syntax a[slice,slice] returns a new array with elements from the sliced ranges,
# where "slice" is Python's [start[:stop[:step]] format.

# "::-1" returns the rows in reverse. ":" returns the columns as is,
# effectively vertically mirroring the original array so the wanted diagonals are
# lower-right-to-uppper-left.
#
# Then a list comprehension is used to collect all the diagonals.  The range
# is -x+1 to y (exclusive of y), so for a matrix like the example above
# (x,y) = (4,5) = -3 to 4.
diags = [a[::-1,:].diagonal(i) for i in range(-a.shape[0]+1,a.shape[1])]

# Now back to the original array to get the upper-left-to-lower-right diagonals,
# starting from the right, so the range needed for shape (x,y) was y-1 to -x+1 descending.
diags.extend(a.diagonal(i) for i in range(a.shape[1]-1,-a.shape[0],-1))

# Another list comp to convert back to Python lists from numpy arrays,
# so it prints what you requested.
print [n.tolist() for n in diags]

输出

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

[[0], [4, 1], [8, 5, 2], [9, 6, 3], [10, 7], [11], [3], [2, 7], [1, 6, 11], [0, 5, 10], [4, 9], [8]]

44
我发现了另一个有趣的解决方案。通过观察x和y的组合,可以立即发现行、列、前向和后向对角线。
Column = x     Row = y        F-Diag = x+y   B-Diag = x-y     B-Diag` = x-y-MIN 
  | 0  1  2      | 0  1  2      | 0  1  2      | 0  1  2        | 0  1  2     
--|---------   --|---------   --|---------   --|---------     --|---------    
0 | 0  1  2    0 | 0  0  0    0 | 0  1  2    0 | 0  1  2      0 | 2  3  4     
1 | 0  1  2    1 | 1  1  1    1 | 1  2  3    1 |-1  0  1      1 | 1  2  3     
2 | 0  1  2    2 | 2  2  2    2 | 2  3  4    2 |-2 -1  0      2 | 0  1  2     

从图表中可以看出,每个对角线和轴线都可以使用这些方程式进行唯一标识。从每个表格中取出每个唯一的数字,并为该标识符创建一个容器。
请注意,反向对角线已经偏移以从零索引开始,并且正向对角线的长度始终等于反向对角线的长度。
test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]

max_col = len(test[0])
max_row = len(test)
cols = [[] for _ in range(max_col)]
rows = [[] for _ in range(max_row)]
fdiag = [[] for _ in range(max_row + max_col - 1)]
bdiag = [[] for _ in range(len(fdiag))]
min_bdiag = -max_row + 1

for x in range(max_col):
    for y in range(max_row):
        cols[x].append(test[y][x])
        rows[y].append(test[y][x])
        fdiag[x+y].append(test[y][x])
        bdiag[x-y-min_bdiag].append(test[y][x])

print(cols)
print(rows)
print(fdiag)
print(bdiag)

这将打印

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

使用defaultdict和lambda,这可以进一步泛化:
from collections import defaultdict


def groups(data, func):
    grouping = defaultdict(list)
    for y in range(len(data)):
        for x in range(len(data[y])):
            grouping[func(x, y)].append(data[y][x])
    return list(map(grouping.get, sorted(grouping)))


test = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
cols = groups(test, lambda x, y: x)
rows = groups(test, lambda x, y: y)
fdiag = groups(test, lambda x, y: x + y)
bdiag = groups(test, lambda x, y: x - y)

7
这是我见过的关于这个问题最好的答案。 - Goodword
4
我喜欢这个答案,因为它把灵活性和编码结合起来,并展示了矩阵中元素与它们的 x y 坐标之间的数学关系。 - Susan
B-Diag(最后一张表)的条目没有意义。0-0-min(0,0)= 0。你怎么得到2? - TheRealFakeNews
@TheRealFakeNews MINB-Diag中最小值的常量。0 - 0 - (-2) == 2。这使您可以在列表上下文中使用基于零的数组索引来排列值。 - flakes

22

从上向右倾斜的对角线开始。

如果 (x,y) 是矩阵内的一个矩形坐标,您需要将其转换为/从一个坐标系 (p,q),其中 p 是对角线的数量,q 是沿对角线的索引。 (因此,p=0 是 [-2] 对角线,p=1 是 [9,5] 对角线,p=2 是 [3,-6,3] 对角线,依此类推。)

要将 (p,q) 转换为 (x,y),可以使用以下公式:

x = q
y = p - q

尝试输入p和q的值,以查看它是如何工作的。

现在,您只需循环... 对于从0到2N-1的p和从max(0,p-N+1)到min(p,N-1)的q。将p、q转换为x、y并打印出来。

然后对于其他对角线,重复循环但使用不同的转换:

x = N - 1 - q
y = p - q

(这实际上只是将矩阵左右翻转。)

抱歉,我并没有在Python中编写这个代码。 :-)


3
点击这里查看Python实现。 - BioGeek

19

这是给Moe的,他问了一个类似的问题

我首先编写了简单的函数来复制任何矩形矩阵的行或列。

def get_rows(grid):
    return [[c for c in r] for r in grid]

def get_cols(grid):
    return zip(*grid)

通过这两个函数,我可以通过在每行的开头/结尾添加递增/递减的缓冲区来得到对角线。然后我获取该缓冲网格的列,然后在每个列上移除缓冲区。

1 2 3    |X|X|1|2|3|    | | |1|2|3|
4 5 6 => |X|4|5|6|X| => | |4|5|6| | => [[7],[4,8],[1,5,9],[2,6],[3]]
7 8 9    |7|8|9|X|X|    |7|8|9| | |

.

def get_backward_diagonals(grid):
    b = [None] * (len(grid) - 1)
    grid = [b[i:] + r + b[:i] for i, r in enumerate(get_rows(grid))]
    return [[c for c in r if c is not None] for r in get_cols(grid)]

def get_forward_diagonals(grid):
    b = [None] * (len(grid) - 1)
    grid = [b[:i] + r + b[i:] for i, r in enumerate(get_rows(grid))]
    return [[c for c in r if c is not None] for r in get_cols(grid)]

8

最近我不得不重新发明这个轮子。以下是一种易于重用/扩展的方法,用于查找正方形列表中的对角线:

def get_diagonals(grid, bltr = True):
  dim = len(grid)
  assert dim == len(grid[0])
  return_grid = [[] for total in xrange(2 * len(grid) - 1)]
  for row in xrange(len(grid)):
    for col in xrange(len(grid[row])):
      if bltr: return_grid[row + col].append(grid[col][row])
      else:    return_grid[col - row + (dim - 1)].append(grid[row][col])
  return return_grid

假设列表索引如下:

00 01 02 03

10 11 12 13

20 21 22 23

30 31 32 33

那么设置 bltr = True(默认值)将返回从左下到右上的对角线,即:
00           # row + col == 0
10 01        # row + col == 1
20 11 02     # row + col == 2
30 21 12 03  # row + col == 3
31 22 13     # row + col == 4
32 23        # row + col == 5
33           # row + col == 6

bltr = False,返回从左下到右上的对角线,即:

30            # (col - row) == -3
20 31         # (col - row) == -2
10 21 32      # (col - row) == -1
00 11 22 33   # (col - row) == 0
01 12 23      # (col - row) == +1
02 13         # (col - row) == +2
03            # (col - row) == +3

这里有一个可运行的版本,使用OP提供的输入矩阵。请点击链接:https://repl.it/JuZ3

2
我猜现在有更简单的方法来完成这个操作。(但只有在您已经熟悉上面的答案时才使用它)。
from collections import defaultdict

有一种叫做defaultdict的方法,它是从collections模块导入的,用于创建字典,如果你不知道将要拥有的键。

我们在以下情况下使用它:

  • 如果您不知道键,但想要为特定键分配某个值。
  • 普通字典会在字典中不存在该键时引发keyerror。 但是这个不会(如果您想,可以将某个函数分配给它)

导入后,您可以运行以下代码并检查。

rows,cols = 3,3
matrix = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]

diagonal1 = defaultdict(list) # For the top right to bottom left
diagonal2 = defaultdict(list) # For the top left to bottom right
for i in range(rows):
    for j in range(cols):
        diagonal1[i-j].append(matrix[i][j])
        diagonal2[i+j].append(matrix[i][j])
print(diagonal1,'\n',diagonal2)

list参数将创建一个特定键的值列表。

输出如下:

defaultdict(<class 'list'>, {0: [1, 5, 9], -1: [2, 6], -2: [3], 1: [4, 8], 2: [7]}) 
defaultdict(<class 'list'>, {0: [1], 1: [2, 4], 2: [3, 5, 7], 3: [6, 8], 4: [9]})

现在您可以自由使用两条对角线。要了解更多关于defaultdict的信息,请使用此链接:点击这里

1
这仅适用于宽度和高度相等的矩阵。 但它也不依赖于任何第三方。
matrix = [[11, 2, 4],[4, 5, 6],[10, 8, -12]]

# only works for diagnoals of equal width and height
def forward_diagonal(matrix):
    if not isinstance(matrix, list):
        raise TypeError("Must be of type list")

    results = []
    x = 0
    for k, row in enumerate(matrix):
        # next diag is (x + 1, y + 1)
        for i, elm in enumerate(row):

            if i == 0 and k == 0:
                results.append(elm)
                break
            if (x + 1 == i):
                results.append(elm)
                x = i
                break
    return results

print 'forward diagnoals', forward_diagonal(matrix)

1

基于Nemo上面的答案的代码:

def print_diagonals(matrix):
    n = len(matrix)
    diagonals_1 = []  # lower-left-to-upper-right diagonals
    diagonals_2 = []  # upper-left-to-lower-right diagonals
    for p in range(2*n-1):
        diagonals_1.append([matrix[p-q][q] for q in range(max(0, p - n + 1), min(p, n - 1) + 1)])
        diagonals_2.append([matrix[n-p+q-1][q] for q in range(max(0, p - n + 1), min(p, n - 1) + 1)])
    print("lower-left-to-upper-right diagonals: ", diagonals_1)
    print("upper-left-to-lower-right diagonals: ", diagonals_2)


print_diagonals([
    [1, 2, 1, 1],
    [1, 1, 4, 1],
    [1, 3, 1, 6],
    [1, 7, 2, 5],
])

lower-left-to-upper-right diagonals:  [[1], [1, 2], [1, 1, 1], [1, 3, 4, 1], [7, 1, 1], [2, 6], [5]]
upper-left-to-lower-right diagonals:  [[1], [1, 7], [1, 3, 2], [1, 1, 1, 5], [2, 4, 6], [1, 1], [1]]

1
使用itertools
matrix = [[-2,  5,  3,  2],
      [ 9, -6,  5,  1],
      [ 3,  2,  7,  3],
      [-1,  8, -4,  8]]

import itertools as it

def show_diagonals(alist):

    # get row/col lenght
    a = len(alist)

    # creating a fliped matrix
    rlist = []
    for r in alist:
        new = r.copy()
        new.reverse()
        rlist.append(new)

    flatten_list = list(it.chain.from_iterable(alist)) 
    flatten_rlist = list(it.chain.from_iterable(rlist)) 
    b = len(flatten_list)
    first_diag = list(it.islice(flatten_list, 0, b+1, a+1))
    second_diag = list(it.islice(flatten_rlist, 0, b+1, a+1))
    return first_diag, second_diag

a, b = show_diagonals(matrix)

1
尝试使用字典。
mat = [[-2,  5,  3,  2],
      [ 9, -6,  5,  1],
      [ 3,  2,  7,  3],
      [-1,  8, -4,  8]]
dct = dict()
for i in range(len(mat)-1,-len(mat[0]),-1):
    dct[i] = []
for i in range(len(mat)):
    for j in range(len(mat[0])):
       dct[i-j].append(mat[i][j])
print(dct)

输出:

{3: [-1], 2: [3, 8], 1: [9, 2, -4], 0: [-2, -6, 7, 8], -1: [5, 5, 3], -2: [3, 1], -3: [2]}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接