一些bug,我两天都找不到(输出不符合预期)


from numpy import *
import time
import pylab as plt
a=zeros([10,10])
b=ones([10,10])
dx=dy=0.1
x,y = mgrid[:1:dx,:1:dy]
def add(a,b):
  a[1:-1,1:-1]=a[1:-1,1:-1]+b[1:-1,1:-1]
  data=0
  print(data)
for i in range(4):
#data.append(a)
    print (a)
    add(a,b)

上面的

是澄清我遇到的问题的说明代码。代码输出的内容是:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  1.  1.  1.  1.  1.  1.  1.  1.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  2.  2.  2.  2.  2.  2.  2.  2.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  3.  3.  3.  3.  3.  3.  3.  3.  0.]
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

但是代码输出:

[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  4.  4.  4.  4.  4.  4.  4.  4.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

那么这种问题有解决办法吗?

谢谢~

有人提出了以下解决方案:

from numpy import*
import time
import pylab as plt
dx=dy=0.1
x,y = mgrid[:1:dx,:1:dy]
data=[]
for i in range(4):
    a=zeros([10,10])
    b=ones([10,10])
    a[1:-1,1:-1] = b[1:-1,1:-1]*i
    data.append(a)
for k in range(4):
     print(data[k])

然而,这并不是解决这类问题的典型方法。这是因为如果我使用的[add]函数或示例中的a, b数组发生变化,则此解决方案将失去其效果

换句话说,该代码给出了所需的输出:

for _ in range(4):
    print(a)
    add(a, b)

但这不是:

data = []
for _ in range(4):
    data.append(a)
    add(a, b)
for d in data: 
    print(d)

让我们先看一个简单的例子,它将帮助你理解发生了什么:

In [184]: a = zeros(3)
In [185]: a
Out[185]: array([ 0.,  0.,  0.])
In [186]: b = a
In [187]: b[0] = 99
In [188]: print(a)
[ 99.   0.   0.]

你知道发生了什么吗?我们使一个数组b与数组a相同,然后修改数组b也改变了数组a !

之所以会发生,是因为实际上只有一个数组对象!"变量"ab只是同一个底层对象的两个名称。因此,我们可以通过使用名称b来修改数组,如果我们使用名称a,我们也会看到变化,因为它是相同的数组。

如果将a附加到列表中,也会发生相同的情况。该列表包含对对象的引用。但是在您的例子中,所有引用都指向同一个对象!

解决这个问题的一种方法是每次将数组添加到列表后创建一个新的数组对象:
data = []
for _ in range(4):
    data.append(a.copy())
    add(a, b)

相关内容

  • 没有找到相关文章

最新更新