为什么它打印两次 "big" 对角线(矩阵)

Why does it print twice the "big" diagonal (matrix)

我有一个如下所示的矩阵:

`
matrix = 
[
['P', 'o', 'P', 'o', 'P'],
['m', 'i', 'c', 's', 'r'],
['g', 'a', 'T', 'A', 'C'],
['n', 'n', 'e', 'r', 't'],
['a', 'g', 'o', 'd', 'o'],
['a', 'p', 'p', 'l', 'e']
    ]`

以及以 'diagonal rising to the right' 方式重复打印每个字母的代码:

`test_word = ''
for upper in range(len(matrix)):
    for rep1 in range(min(upper + 1, len(matrix[0]))):
        for rep2 in range(rep1, min(upper + 1, len(matrix[0]))):
            for j in range(rep1, rep2 + 1):
                test_word += (matrix[upper - j][j])
                print(test_word)
            test_word = ''`

输出:

 `
P,m,m,mo,o,g,g,gi
......when it arrives to the diagonal anTsP here is the output:
a,a,an,a,an,anT,a,an,anT,anTs,a,an,anT,anTs,anTsP` 

问题是它重复了两次 a, an, anT 和 Ts... 如果您还不了解该模式,我想遍历每条对角线并尝试一直找到 "spell" 字母,因此例如我对 anTsP 对角线的理想输出是:

a, an, anT, anTs, anTsP, n,nT,nTs,nTsP, T, etc.

如果你有什么想法,

您的 print 语句中的缩进似乎有误:

test_word = ''
for upper in range(len(matrix)):
    for rep1 in range(min(upper + 1, len(matrix[0]))):
        for rep2 in range(rep1, min(upper + 1, len(matrix[0]))):
            for j in range(rep1, rep2 + 1):
                test_word += (matrix[upper - j][j])
            print(test_word, end=' ')  # <--- indent left
            test_word = ''

但这只打印:

P m mo o g gi giP i iP P n na nac naco a ac aco c co o a an anT anTs anTsP n nT nTs nTsP T Ts TsP s sP P a ag age ageA ageAr g ge geA geAr e eA eAr A Ar r

不是 'porC, 'pdt', 'lo', 'e' 对角线。


此示例打印所有对角线:

matrix = [
['P', 'o', 'P', 'o', 'P'],
['m', 'i', 'c', 's', 'r'],
['g', 'a', 'T', 'A', 'C'],
['n', 'n', 'e', 'r', 't'],
['a', 'g', 'o', 'd', 'o'],
['a', 'p', 'p', 'l', 'e']
]

def rotate(l, n):
    return l[n:] + l[:n]

def get_substrings(s):
    for j in range(0, len(s)):
        for i in range(j+1, len(s)+1):
            yield s[j:i]

def get_values(matrix):
    transposed = [*zip(*matrix)]
    for i in range(len(matrix[0])):
        transposed[i] = rotate(transposed[i], -i)
    transposed = [*zip(*transposed)]
    for i, lst in enumerate(transposed, 1):
        yield from get_substrings(''.join(lst[:i]))
    for i, lst in enumerate(transposed, 1):
        yield from get_substrings(''.join(lst[i:]))

for v in get_values(matrix):
    print(v, end=' ')

打印:

P m mo o g gi giP i iP P n na nac naco a ac aco c co o a an anT anTs anTsP n nT nTs nTsP T Ts TsP s sP P a ag age ageA ageAr g ge geA geAr e eA eAr A Ar r p po por porC o or orC r rC C p pd pdt d dt t l lo o e

编辑:没有 yield 的版本:

matrix = [
['P', 'o', 'P', 'o', 'P'],
['m', 'i', 'c', 's', 'r'],
['g', 'a', 'T', 'A', 'C'],
['n', 'n', 'e', 'r', 't'],
['a', 'g', 'o', 'd', 'o'],
['a', 'p', 'p', 'l', 'e']
]

def rotate(l, n):
    return l[n:] + l[:n]

def get_substrings(s):
    rv = []
    for j in range(0, len(s)):
        for i in range(j+1, len(s)+1):
            rv.append(s[j:i])
    return rv

def get_values(matrix):
    rv = []
    transposed = [*zip(*matrix)]
    for i in range(len(matrix[0])):
        transposed[i] = rotate(transposed[i], -i)
    transposed = [*zip(*transposed)]
    for i, lst in enumerate(transposed, 1):
        rv.extend(get_substrings(''.join(lst[:i])))
    for i, lst in enumerate(transposed, 1):
        rv.extend(get_substrings(''.join(lst[i:])))
    return rv

for v in get_values(matrix):
    print(v, end=' ')