在对角线上打印2D数组 [英] Print 2d array on diagonals

查看:222
本文介绍了在对角线上打印2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从左下角开始向右上角移动的二维数组的对角线.我已经设法打印出矩阵的前半部分,但是当我不得不打印出矩阵的第二部分时,我陷入了困境,我希望有人能给我一个提示,以继续.这是我所拥有的:

I'm trying to print the diagonals for a 2d array starting with the bottom left corner and moving towards the top right corner. I've managed to print the first half of the matrix but I got stuck when I have to print the second part of it and I'm hoping somebody can give me a clue how to continue. Here is what I have:

matrix = [["A", "B", "C", "D"], 
          ["E", "F", "G", "H"], 
          ["I", "J", "K", "L"],
          ["M", "N", "O", "P"],
          ["Q", "R", "S", "T"]]

和将对角线打印到一个点的偏函数:

and the partial function that print the diagonals up to a point:

def diagonal_matrix_print(input_matrix):
    width = len(input_matrix[0])
    height = len(input_matrix)
    start_row = height - 1
    first_row = 0 
    for start_row in reversed(range(0, height)):
        i = start_row
        for column in range(0, width):
            if i == height:
                start_row = start_row - 1
                break
            print input_matrix[i][column]
            i = i + 1
        print

我面临的问题是打印从矩阵的后半部分开始的对角线-B G L, C H, D

The issue I'm facing is printing the diagonals that start with the second half of the matrix - B G L, C H, D

我尝试使用另外2个for循环,例如:

I tried using another 2 for loops for it like:

for row in range (0, height -1):
    i = row
    for start_column in range(1, width):
        print input_matrix[i][start_column]
        i = i + 1

但是当行值更改为1时,不再打印对角线...

but when the row value changes to 1, is not printing the diagonal anymore...

推荐答案

假设我们有列表列表L:

>>> L = [[ 0,  1,  2],
         [ 3,  4,  5],
         [ 6,  7,  8],
         [ 9, 10, 11]])

我们想要一个diagonals这样的函数

We want a function diagonals such that

>>> diagonals(L)
[[9], [6, 10], [3, 7, 11], [0, 4, 8], [1, 5], [2]]

我们可以考虑2个不同坐标下的L中的项目 系统.有通常的(x, y)坐标系,其中(x, y)对应于 值为L[x][y]的位置.

We can think about items in L with respect to 2 different coordinate systems. There is the usual (x, y) coordinate system where (x, y) corresponds to the location with value L[x][y].

然后还有(p, q)坐标系,其中p表示 pth对角线,其中p=0左下角的对角线.和q 表示沿pth诊断的qth项目,q=0左侧开始 边缘.因此,(p, q) = (0,0)对应于值为L[3][0] = 9的位置 在上面的示例中.

And then there is also the (p, q) coordinate system where p represents the pth diagonal, with p=0 being the diag at the lower-left corner. And q represents the qth item along the pth diag, with q=0 starting at the left edge. Thus (p, q) = (0,0) corresponds to the location with value L[3][0] = 9 in the example above.

h,w分别等于L的高度和宽度. 然后p的范围是0h + w - 1.

Let h,w equal the height and width of L respectively. Then p ranges from 0 to h + w - 1.

我们想要一个将(p, q)坐标转换为(x, y)坐标的公式.

We want a formula for translating from (p, q) coordinates to (x, y) coordinates.

  • x随着p的增加而线性减小.
  • x随着q的增加而线性增加.
  • (p, q) = (0, 0)时,x等于h.
  • x decreases linearly as p increases.
  • x increases linearly as q increases.
  • When (p, q) = (0, 0), x equals h.

因此:x = h - p + q.

  • y不会随p更改(如果已修复q).
  • y随着q的增加而线性增加.
  • (p, q) = (0, 0)时,y等于q.
  • y does not change with p (if q is fixed).
  • y increases linearly as q increases.
  • When (p, q) = (0, 0), y equals q.

因此,y = q.

现在xy的有效值范围要求:

Now the extent of valid values for x and y requires that:

(0 <= x = h - p + q < h) and (0 <= y = q < w)

等效于

(p - h + 1 <= q < p + 1) and (0 <= q < w)

等效于

max(p - h + 1, 0) <= q < min(p + 1, w)

因此,我们可以使用

for p in range(h + w - 1):
    for q in range(max(p-h+1, 0), min(p+1, w))
        L[h - p + q - 1][q]


def diagonals(L):
    h, w = len(L), len(L[0])
    return [[L[h - p + q - 1][q]
             for q in range(max(p-h+1, 0), min(p+1, w))]
            for p in range(h + w - 1) ]


matrix = [ ["A", "B", "C", "D"], ["E","F","G","H"], ["I","J","K","L"], ["M","N","O","P"], ["Q", "R", "S","T"]]

for diag in diagonals(matrix):
    print(diag)

收益

['Q']
['M', 'R']
['I', 'N', 'S']
['E', 'J', 'O', 'T']
['A', 'F', 'K', 'P']
['B', 'G', 'L']
['C', 'H']
['D']

这篇关于在对角线上打印2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆