英文:
Matrix problem that finds the required matrix that when summed up to get a matrix of prime numbers
问题
所以我已经在Python中编写了一段代码,用于计算我应该将1添加到n*m矩阵中的每个元素多少次,以使它们全部变为素数。以下是代码:
def isPrime(n):
if n == 2:
return True
if n % 2 == 0 or n == 1 or n == 0 or n < 0:
return False
for i in range(3, int((n ** 0.5)) + 1, 2):
if n % i == 0:
return False
return True
n, m = map(int, input().split())
l = []
for i in range(n):
l.append([int(x) for x in input().split()])
mtxsum = [[0] * m] * n
for i in range(n):
for j in range(m):
init = l[i][j]
if not isPrime(l[i][j]):
while isPrime(l[i][j]) != True:
if l[i][j] == 1:
l[i][j] += 1
elif l[i][j] % 2 == 0:
l[i][j] += 1
else:
l[i][j] += 2
mtxsum[i][j] = (l[i][j] - init)
print(mtxsum[i][j])
print(mtxsum)
现在,我在这部分感到困惑:
mtxsum[i][j] = (l[i][j] - init)
print(mtxsum[i][j])
print(mtxsum)
在这里,不仅仅是改变了位置[0][0]的元素,还改变了位置[1][0],[2][0]等位置的元素,最终得到矩阵:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
但我应该得到:
[[1, 0, 0], [0, 1, 1], [1, 1, 1]]
我错过了/做错了什么?请注意,在开始时,求和矩阵是一个On*m矩阵(仅带有0元素)。
在你的代码中,问题出现在创建mtxsum
矩阵的方式上。你使用了以下方式来创建mtxsum
矩阵:
mtxsum = [[0] * m] * n
这实际上创建了一个包含相同子列表的列表,因此所有子列表都指向相同的内存地址。当你修改其中一个子列表的元素时,其他子列表也会受到影响,因为它们实际上是同一个列表的引用。
要解决这个问题,你可以使用列表推导来创建mtxsum
矩阵,确保每个子列表都是独立的,像这样:
mtxsum = [[0 for _ in range(m)] for _ in range(n)]
这将创建一个包含n个子列表的列表,每个子列表包含m个0。这样,你就可以单独修改每个元素而不影响其他元素。
英文:
so I've written a code in Python that counts how many times I should add 1 to every element in a n*m matrix to get all of them to be prime. Here is the code:
def isPrime(n):
if n == 2:
return True
if n % 2 == 0 or n == 1 or n == 0 or n < 0:
return False
for i in range(3, int((n ** 0.5)) + 1, 2):
if n % i == 0:
return False
return True
n, m = map(int, input().split())
l = []
for i in range(n):
l.append([int(x) for x in input().split()])
mtxsum = [[0] * m] * n
for i in range(n):
for j in range(m):
init = l[i][j]
if not isPrime(l[i][j]):
while isPrime(l[i][j]) != True:
if l[i][j] == 1:
l[i][j] += 1
elif l[i][j] % 2 == 0:
l[i][j] += 1
else:
l[i][j] += 2
mtxsum[i][j] = (l[i][j] - init)
print(mtxsum[i][j])
print(mtxsum)
Now, I get confused at this part:
mtxsum[i][j] = (l[i][j] - init)
print(mtxsum[i][j])
print(mtxsum)
where instead of changing for example only the element at the position [0][0], the elements at the positions [1][0], [2][0], ... are being changed as well, getting at the end the matrix:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
but I should get:
[[1, 0, 0], [0, 1, 1], [1, 1, 1]]
What am I missing/doing wrong? Note that at the start, the matrix of sum is a matrix On*m (only with 0 elements)
答案1
得分: 0
当你创建一个类似mtxsum = [[0] * m] * n
的矩阵时,如果你尝试更改任何一个单独的元素,矩阵中的每个元素都会发生变化。这就是为什么矩阵“mtxsum”中的每个元素都被覆盖为“1”的原因。
所以要手动使用迭代创建矩阵。
mtxsum = []
for i in range(n):
temp = []
for j in range(m):
temp.append(0)
mtxsum.append(temp)
现在你可以继续进行了。
英文:
when you create a matrix like mtxsum = [[0] * m] * n
, every element in matrix changes if you try to change any single element. That is why every element in matrix "mtxsum" overrides to "1".
So create matrix manually with iterations.
mtxsum = []
for i in range(n):
temp = []
for j in range(m):
temp.append(0)
mtxsum.append(temp)
Now you are good to go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论