我是python的学生和入门者,我遇到了问题。我从矩阵6乘6做一些运算。我必须为每个部分的每个值(11 * matrix_val)%37做,但是我的计数器工作不正常...
这是我的代码:
m = [[36, 19, 27, 36, 7, 10], [2, 18, 3, 33, 2, 21], [26, 27, 4, 22, 30, 31], [29, 36, 7, 20, 6, 30], [30, 6, 14, 23, 15, 13], [22, 10, 10, 35, 15, 22]]
i = 0 #matrix jumping
j = 0 #scan the matrix
k = 1
somme = 0
while k < 24:
while i < 6:
while j < 6:
m[i][j] = (11 * m[i][j])%37
print(i,j,k)
j = j + 1
i = i + 1
k = k + 1
j = 0
i = 0
print(m)
但是当我点我的计数器时,只有“ j”在增加,而“ i”和“ k”却没有,在这里您可以看到:
isatix@isatix-MS-7A34:~/python/ceinture/20 points$ python3 matrice.py
0 0 1
0 1 1
0 2 1
0 3 1
0 4 1
0 5 1
[[26, 24, 1, 26, 3, 36], [2, 18, 3, 33, 2, 21], [26, 27, 4, 22, 30, 31], [29, 36, 7, 20, 6, 30], [30, 6, 14, 23, 15, 13], [22, 10, 10, 35, 15, 22]]
isatix@isatix-MS-7A34:~/python/ceinture/20 points$
所以也许我在做错事,但我看不到^^ 有人有想法吗?
编辑:问题已解决:
这是可以正常工作的代码:
m = [[36, 19, 27, 36, 7, 10], [2, 18, 3, 33, 2, 21], [26, 27, 4, 22, 30, 31], [29, 36, 7, 20, 6, 30], [30, 6, 14, 23, 15, 13], [22, 10, 10, 35, 15, 22]]
i = 0 #saute de matrice en matrice
j = 0 #parcour la matrice
k = 1
somme = 0
while k < 24:
i = 0
j = 0
while i < 6:
while j < 6:
m[i][j] = (11 * m[i][j])%37
print(i,j,k)
j = j + 1
i = i + 1
j = 0
k = k + 1
print(m)
答案 0 :(得分:1)
您在错误的位置将i
和j
设置为等于0。处理3个循环之前,您应该熟悉2个循环。另外,您的k
应该从1还是0开始?
m = [[36, 19, 27, 36, 7, 10],
[2, 18, 3, 33, 2, 21],
[26, 27, 4, 22, 30, 31],
[29, 36, 7, 20, 6, 30],
[30, 6, 14, 23, 15, 13],
[22, 10, 10, 35, 15, 22]]
k = 1
somme = 0
while k < 24:
i = 0 #matrix jumping
while i < 6:
j = 0 #scan the matrix
while j < 6:
m[i][j] = (11 * m[i][j])%37
print(i,j,k)
j += 1
i += 1
k += 1
print(m)