使用等号 python 代码没有给出我想要的结果
the code does not give the results what I want using equal sign in python
我有这样的代码。
import numpy as np
a = np.zeros(shape = (4,4))
a+= 2
b = np.zeros(shape = (4,4))
b+=2
t = 0
while t<2:
for i in range(1,3):
for j in range(1,3):
if a[i,j] == a[i-1,j]:
b[i,j] = a[i,j]+1
print(a,t)
print(b,t)
a = b
t+= 1
我希望在 t = 2
a = [2 2 2 2, 2 3 3 2, 2 3 3 2, 2 2 2 2] 和 b = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2]
但实际上在 运行 的末尾 a = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2]
有人知道为什么吗?是因为我声明 a = b 吗?如果是,有什么办法吗?
谢谢..
用 a[:,:]=b
(复制元素b
变成 a
).
我有这样的代码。
import numpy as np
a = np.zeros(shape = (4,4))
a+= 2
b = np.zeros(shape = (4,4))
b+=2
t = 0
while t<2:
for i in range(1,3):
for j in range(1,3):
if a[i,j] == a[i-1,j]:
b[i,j] = a[i,j]+1
print(a,t)
print(b,t)
a = b
t+= 1
我希望在 t = 2 a = [2 2 2 2, 2 3 3 2, 2 3 3 2, 2 2 2 2] 和 b = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2] 但实际上在 运行 的末尾 a = [2 2 2 2, 2 3 3 2, 2 4 4 2, 2 2 2 2]
有人知道为什么吗?是因为我声明 a = b 吗?如果是,有什么办法吗? 谢谢..
用 a[:,:]=b
(复制元素b
变成 a
).