gpt4 book ai didi

python - 为什么它打印两倍的 "big"对角线(矩阵)

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:05 27 4
gpt4 key购买 nike

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

`
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']
]`

这段代码以“对角线向右上升”的方式重复打印每个字母:

`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 anTs...如果您不理解该模式,我想遍历每个对角线并尝试找到所有“拼写”字母的方法,因此例如我对 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=' ')

关于python - 为什么它打印两倍的 "big"对角线(矩阵),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59120492/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com