问题
I have a matrix like the following:
`
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']
]`
and this code which prints every letter in a 'diagonal rising to the right' way with repetitions:
`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 = ''`
output:
`
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`
The problem is that it repeats twice a, an, anT anTs... If you haven't understand the pattern, I want to go through each diagonal and try to find all the way to "spell" the letter, so for example my ideal output for the anTsP diagonal would be:
a, an, anT, anTs, anTsP, n,nT,nTs,nTsP, T, etc.
If you have any ideas,
回答1:
It appears that you have bad indentation in your print
statement:
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 = ''
But this however prints only:
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
Not the 'porC, 'pdt', 'lo', 'e'
diagonals.
This example prints all diagonals:
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=' ')
Prints:
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
Edit: version without 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=' ')
来源:https://stackoverflow.com/questions/59120492/why-does-it-print-twice-the-big-diagonal-matrix