List Python中二维列表不同点的对角线

List Python中二维列表不同点的对角线,list,python-2.7,List,Python 2.7,我想知道如何得到不通过中心的对角线的值列表 假设我有一个嵌套列表: L = [[1,2,3],[4,5,6][7,8,9]] 比如说,如何得到对角线[2,6]?我不太确定您想要什么,但这段代码提供了每个方向上的所有完整对角线: L = [[1,2,3],[4,5,6], [7,8,9]] # number of rows, number of columns: ie L is m x n m, n = len(L), len(L[0]) # Retreive the NE-SW (diag1

我想知道如何得到不通过中心的对角线的值列表

假设我有一个嵌套列表:

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

比如说,如何得到对角线[2,6]?

我不太确定您想要什么,但这段代码提供了每个方向上的所有完整对角线:

L = [[1,2,3],[4,5,6], [7,8,9]]
# number of rows, number of columns: ie L is m x n
m, n = len(L), len(L[0])

# Retreive the NE-SW (diag1) and NW-SE (diag2) diagonals
diag1 = []
diag2 = []
for p in range(m+n-1):
    diag1.append([])
    diag2.append([])
    q1 = 0
    if p >= n:
        q1 = p - n + 1
    q2 = m
    if p < m-1:
        q2 = p+1
    for q in range(q1, q2):
        x, y = p - q, q
        diag1[-1].append(L[y][x])
        # To get the other diagonal, read each row "backwards"
        x = n - x - 1
        diag2[-1].append(L[y][x])
print 'diag1:', diag1
print 'diag2:', diag2

您将如何修改代码以接受具有以下特征的非mxn嵌套列表:len(L[i])>len(L[i+1])

例如,以下嵌套列表:

[[1,2,3,4,5,6,7,8,9,10],
[11,12,13,14,15,16,17], 
[18,19,20,21,22], 
[23,24,25,26], 
[27,28], 
[29]]
应产生:

[[1], 
[2,11], 
[3,12,18], 
[4,13,19,23], 
[5,14,20,24,27], 
[6,15,21,25,28,29], 
[7,16,22,26], 
[8,17], 
[9], 
[10]

你说的“不要穿过中心”是什么意思?我的意思是对角线不需要穿过这个2D列表中的点2,2。如何获得偏离[1,5,8]或[3,5,7]的对角线?抓住所有对角线并删除长度为n(在您的示例中为3)的对角线,但我不确定如何获得偏离中心的对角线的值。
[[1], 
[2,11], 
[3,12,18], 
[4,13,19,23], 
[5,14,20,24,27], 
[6,15,21,25,28,29], 
[7,16,22,26], 
[8,17], 
[9], 
[10]