【问题标题】:How to get diagonals of nested list that pass through a given point - Python如何获得通过给定点的嵌套列表的对角线 - Python
【发布时间】:2018-12-25 23:08:26
【问题描述】:

我有一个这样的二维列表:

thelist=[[0,0,0,0,0,0],
         [1,0,0,0,0,0],
         [0,1,0,0,0,0],
         [0,0,1,0,0,0], # (3,2) is the 1 in this row
         [0,0,0,1,0,0],
         [0,0,0,0,1,0]]

我正在尝试获取通过给定坐标的 2d 列表的 2 条对角线。在上面的列表中,如果坐标是(3,2),那么这两个列表就是[1,1,1,1,1][0,0,1,0,0,0]

我已经看到了可以获取所有对角线或特定对角线的解决方案,但我无法在线找到可以获取通过一个点的对角线的解决方案。这是我尝试过的:

def find_diagonals(thelist,coor):
    twodiag=[[],[]]
    coor1=[coor[0]-min(coor),coor[1]-min(coor)]
    coor2=[coor[0]-min(coor),coor[1]+min(coor)]
    while True:
       try: 
           twodiag[0].append(thelist[coor1[0]][coor1[1]]) 
           coor1[0]+=1
           coor1[1]+=1
       except IndexError:
           break

    while True:
       try: 
           twodiag[1].append(thelist[coor2[0]][coor2[1]]) 
           coor2[0]+=1
           coor2[1]-=1
       except IndexError:
           break
    return twodiag

但是,它只正确返回一个对角线。如何修复此代码或以其他方式解决问题?如果有任何不清楚的地方,我很乐意在 cmets 中回答。谢谢!

【问题讨论】:

  • 主对角线怎么可能是[1,1,1,1,1,1]???不在主对角线上的值都是零吗?
  • 坐标 (3,2) 不在主对角线上(它在全 1 的对角线上)。
  • 应该!抱歉打错了
  • 无论如何,即使不在主对角线上,您的代码也可以工作
  • 你的第二条对角线不应该是 [0, 0, 1, 0, 0, 0] 而不是 [0, 1, 0, 0, 0] 吗? (不小心把之前的评论删了抱歉。)

标签: python python-3.x list multidimensional-array


【解决方案1】:

使用numpy的解决方案:

import numpy as np
thelist=[[0,0,0,0,0,0],
         [1,0,0,0,0,0],
         [0,1,0,0,0,0],
         [0,0,1,0,0,0], # (3,2) is the 1 in this row
         [0,0,0,1,0,0],
         [0,0,1,0,1,0]]
lst = matrix = np.array(thelist)

i, j = 3, 2 #position of element
major = np.diagonal(lst, offset=(j - i))
print(major)

minor = np.diagonal(np.rot90(lst), offset=-lst.shape[1] + (j + i) + 1)
print(minor)

输出:

[1 1 1 1 1]
[0 0 0 1 0 0]

【讨论】:

  • 酷!谢谢!
【解决方案2】:
width, height = len(thelist[0]), len(thelist)
size = max(width, height)
valid_x, valid_y = range(width), range(height)

pos = (3,2)
x, y = pos

diag1 = [thelist[i][i-x+y] for i in range(size) if i-x+y in valid_y]
diag2 = [thelist[i][x+y-i] for i in range(size) if x+y-i in valid_y]
print (diag1, diag2)
# [1, 1, 1, 1, 1] [0, 0, 0, 1, 0, 0]

【讨论】:

  • 谢谢!但是,输出与预期输出不同([1,1,1,1,1][0,0,1,0,0,0])。
  • 抱歉...现在应该修复
【解决方案3】:

最好的解决方案是简单的线性函数:

def find_diagonals(input_list, coordinates):
    diagonals = [[], []]
    row_coordinate, column_coordinates = coordinates
    for row_number, row in enumerate(input_list):
        diagonal_point = column_coordinates-row_coordinate+row_number
        if 0 <= diagonal_point <= len(row):
            diagonals[0].append(row[diagonal_point])
        diagonal_point = column_coordinates+row_coordinate-row_number
        if 0 <= diagonal_point <= len(row):
            diagonals[1].append(row[diagonal_point])
    return diagonals

【讨论】:

  • 谢谢!该代码适用于 (3,2),但它是 (2,3) 的 IndexErroring
【解决方案4】:

你可以这样计算偏移量:

def get_diagonals(alist, coordinates):
    # Diagonal 1
    start = coordinates
    while start[0] > 0 and start[1] > 0:
        start = (start[0] - 1, start[1] - 1)

    diag1 = []
    index = start
    while index[0] < len(alist) and index[1] < len(alist[0]):
        diag1.append(alist[index[0]][index[1]])
        index = (index[0] + 1, index[1] + 1)

    # Diagonal 2
    start = coordinates
    while start[0] < len(alist) - 1 and start[1] > 0:
        start = (start[0] + 1, start[1] - 1)

    diag2 = []
    index = start
    while index[0] >= 0 and index[1] < len(alist[0]):
        diag2.append(alist[index[0]][index[1]])
        index = (index[0] - 1, index[1] + 1)

    return diag1, diag2


thelist=[[0,0,0,0,0,0],
         [1,0,0,0,0,0],
         [0,1,0,0,0,0],
         [0,0,1,0,0,0], # (3,2) is the 1 in this row
         [0,2,0,1,0,0],
         [3,0,0,0,1,0]]

coord = (3,2)

get_diagonals(thelist, coord)
> ([1, 1, 1, 1, 1], [3, 2, 1, 0, 0, 0])

代码是直截了当的,并逐一计算矩阵中对角线上的位置。

编辑:修正了一个错误

【讨论】:

  • 谢谢!只有一个问题:我怎样才能使此代码适用于坐标(5,5)。现在它给出了一个索引错误
  • 对不起,我的代码中有一个错误。现在它应该可以工作了。
猜你喜欢
  • 1970-01-01
  • 2022-01-10
  • 2020-10-21
  • 2017-05-30
  • 1970-01-01
  • 2018-06-20
  • 2020-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多