将python字典键分配给不同的矩阵

将python字典键分配给不同的矩阵,python,dictionary,matrix,Python,Dictionary,Matrix,在Python2.7中,我试图使用字典为不同的矩阵分配不同的键 编辑--很抱歉以前缺少信息 下面是我的代码的完整的工作示例: import random matrices_dict = {} matrix = [[1,1], [1,1],] def checker(key_number): for x in range(0, 2): for y in range(0, 2): matrix[x][y] = random.rand

在Python2.7中,我试图使用字典为不同的矩阵分配不同的键

编辑--很抱歉以前缺少信息

下面是我的代码的完整的工作示例:

import random
matrices_dict = {}

matrix = [[1,1],
        [1,1],]

def checker(key_number):
    for x in range(0, 2):
        for y in range(0, 2):
            matrix[x][y] = random.randint(0, 9)

    matrices_dict[key_number] = matrix
    print matrices_dict

checker(0)
checker(1)
。。。然后,我看到生成的字典没有存储生成的两个不同的矩阵。相反,它使字典键1和2都对应于第二个矩阵

换句话说,它是在写第一个键的配对。这对我来说很奇怪,因为我很清楚地要求它将新矩阵写入新键

关于如何使其不覆盖第一个密钥/矩阵配对,有什么建议吗

以下是解决问题的方法

。。。我不清楚为什么第二个有效而第一个无效。在第一个例子中,为什么每次调用定义时都需要一个新的矩阵?。。。为什么不重写以前创建的随机工作

不管怎样,为什么矩阵创建的任何细节都会影响字典是否覆盖其先前的键对


出于好奇,我很想知道

好的,我们的想法是生成一个被(调用的)矩阵引用的新对象,并在调用函数时保存对这个新对象的引用(dict key)。例如:

 import numpy as np
 matrices_dict = {}

 def checker(key_number):
      # here we create a new object and add a reference 
      # (key in this case) to this new object to the dict.
      # Note that each time this func is called, this line is executed
      # and thus a new object is created
      matrix = np.random.rand(1,2)
      matrices_dict[key_number] = matrix

 checker(1)
 checker(2)

 print(matrices_dict)
这会产生如下结果:
{1:array([[0.33570685,0.66721728]]),2:array([[0.57074373,0.62535056]])

我们得到两个不同的矩阵,这似乎正是你想要的,对吗

但下一段代码中出现的错误如下。创建一个对象,每次调用函数时,都会将对该对象的新引用添加到dict中

 import numpy as np
 matrices_dict = {}
 # here we create a single object
 # Note that this line gets executed only once, so there is really 
 # just a single object that can be changed and referenced to.
 matrix = np.random.rand(1,2)     

 def checker(key_number):
      # add a new reference (key in this case) to the single object 
      # we created above to the dict
      matrices_dict[key_number] = matrix

 checker(1)
 checker(2)

 print(matrices_dict)
然后我们得到了您描述的错误行为:
{1:array([[0.86548754,0.92694858]]),2:array([[0.86548754,0.92694858]])


关键是理解引用和对象。创建列表时,您创建了一个最初由“矩阵”引用的对象。然后,当函数被调用时,创建了一个新的引用,指向同一个对象!,以字典键的形式。因此,当您使用字典键或“矩阵”时,您最终访问并更改了同一个对象

这些引用是否指向同一对象?矩阵是如何生成的?在我看来,你只是简单地改变了矩阵,而不是构造一个新的。不,我检查过了。它工作得很好。我给出了
matrix=[1,2,3]
作为示例。生成的矩阵可能有问题?或者其他一些问题?显然,您在函数中使用全局值,请打印它们以查看调用
checker(X)
时,如果两者引用相同的对象,会发生什么情况。这就意味着两次都生成了相同的矩阵。你提供的细节太少,我们无法提供太多帮助。请给我一个机会,谢谢你。我的问题很相似,但不太明显,因为我在定义中修改了矩阵,所以我很清楚我没有调用相同的矩阵。我已经在上面编辑了我的评论,其中包含了确切的细节以及最终的效果,但我仍然不能完全确定它为什么会这样,所以如果您对此有想法,那将是很有趣的。当您完全了解引用和对象的工作原理时,这一点就会变得很清楚,因此我建议您查找一些相关信息。这些信息将在您可能遇到的任何编程语言中帮助您。我编辑了我的答案,希望能为你澄清。
 import numpy as np
 matrices_dict = {}
 # here we create a single object
 # Note that this line gets executed only once, so there is really 
 # just a single object that can be changed and referenced to.
 matrix = np.random.rand(1,2)     

 def checker(key_number):
      # add a new reference (key in this case) to the single object 
      # we created above to the dict
      matrices_dict[key_number] = matrix

 checker(1)
 checker(2)

 print(matrices_dict)