附录将覆盖类列表中的现有数据

问题描述 投票:0回答:1

我正在用Python编写数独解决程序。

我有class内部类列表构造函数功能,它们会使list成为对象。

    class Table:

       rows = []

       def __init__(self):
          initRows()

       class Row:

          numbers = []

          def __init__(self):
             "stuff that stores a list of integers in **numbers** list"

       def initRows(self, numbers):
          for i in range(9):
             temp_row = self.Row(i, numbers)
             self.rows.append(temp_row)

程序如下所示:

  1. 创建Table对象时,它会自动尝试使用initRows()函数创建Row对象的9个长度的列表。
  2. initRows()函数中,我们只创建一个类为[[Row的临时对象,然后立即将其[[append()放入rows列表。
  3. [当我们创建Row对象时,它只存储数独表中给定行的给定数字。
  4. 如果我创建了每个临时Row
  5. 临时对象的
  6. numbers
  7. 列表,则其给出正确的值。[0, 0, 0, 7, 4, 0, 0, 0, 6] [4, 0, 6, 8, 0, 0, 5, 0, 7] [7, 0, 0, 0, 9, 0, 0, 0, 4] [0, 3, 0, 9, 8, 4, 7, 0, 0] [8, 2, 0, 6, 1, 3, 4, 0, 9] [0, 4, 0, 0, 0, 0, 3, 0, 0] [0, 6, 2, 3, 7, 0, 0, 0, 5] [0, 0, 5, 4, 0, 9, 0, 0, 0] [0, 7, 0, 0, 6, 1, 2, 0, 8]
但是当我尝试
print()
    for循环
循环一次或多次或初始化完成后,每个
  • Row对象的值时,列表仅填充了最后一个[[Row对象
  • [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] [0, 7, 0, 0, 6, 1, 2, 0, 8] 我在互联网上搜索小时,发现人们对列表类的append()函数有疑问,但没有帮助。
    所以我的问题是:我怎么做这项工作?

    ((如果需要其他信息/代码的一部分:请问!)

    我正在用Python编写数独解决程序。我有一个类,一个内部类,一个列表,构造函数和一个将构成Row对象列表的函数。类表:行= [] ...
    python python-3.x append inner-classes
    1个回答
    0
    投票
    numbers列表是一个[[class属性(意味着它在的所有对象之间共享

    Row类)而不是instance属性。谢谢主席先生!

    © www.soinside.com 2019 - 2024. All rights reserved.