簡體   English   中英

Python將唯一的類對象添加到列表

[英]Python Adding Unique Class Objects to a List

將對象添加到列表時遇到問題。 當我將對象附加到列表的末尾,然后嘗試遍歷列表時,列表上的每個點都只給我返回最近添加的對象。

該腳本比較Excel電子表格中不同項目的信息。 我正在使用適用於Windows的Python和win32com.client來訪問我感興趣的speadsheets。我在Stack Overflow上閱讀了一些其他文章,但在將唯一對象添加到列表時遇到了問題,但是我確定我不會具有與他們相同的錯誤(在循環中初始化列表,在創建類對象時不提供輸入屬性)。

我可以在循環中注釋掉對象創建,只需將數字添加到列表中,就可以打印出所有三個唯一值,但是一旦我再次調用對象創建,就會出問題。 下面的代碼僅顯示三個最新添加的項目。 任何幫助將不勝感激,謝謝!

class Project:
    """
    Creates an instance for each project
    in the spreadsheet
    """

    def __init__(self, bldg, zone, p_num, p_name, p_mgr,
                 const_mgr, ehs_lias, ehs_const, status,
                 p_type, start, finish):
        self.bldg = bldg
        self.zone = zone
        self.p_num = p_num
        self.p_name = p_name
        self.p_mgr = p_mgr
        self.const_mgr = const_mgr
        self.ehs_lias = ehs_lias
        self.ehs_const = ehs_const
        self.status = status
        self.p_type = p_type
        self.start = start
        self.finish = finish

    def quickPrint(self):
            """ prints quick glance projects details """
            if p_name is None:
                pass
            else:
                print 'Building ' + str(bldg.Value)
                print str(p_name.Value)
                print str(p_type.Value) + " -- " + str(p_mgr.Value)
                print str(start.Value) + " - " + str(finish.Value)


projects = []
for i in range(25, 28):
    bldg = excel.Cells(i,1)
    zone = excel.Cells(i,2)
    p_num = excel.Cells(i,3)
    p_name = excel.Cells(i,4)
    p_mgr = excel.Cells(i,5)
    const_mgr = excel.Cells(i,6)
    ehs_lias = excel.Cells(i,7)
    ehs_const = excel.Cells(i,8)
    status = excel.Cells(i,9)
    p_type = excel.Cells(i,10)
    start = excel.Cells(i,11)
    finish = excel.Cells(i,12)
    projects.append(Project(bldg, zone, p_num, p_name, p_mgr,
                        const_mgr, ehs_lias, ehs_const,
                        status, p_type, start, finish))
projects[0].quickPrint()
projects[1].quickPrint()
projects[2].quickPrint()

我認為您定義的quickPrint不正確。 就其而言, p_mgr p_namep_typep_mgr等,因此它將在范圍解析樹或它所調用的任何東西上查找,然后最終找到它們-您在for循環中最后一次定義它們的位置,這就是為什么它為您提供了最后的價值。

因為您在循環中使用了相同的變量名,所以您將隱藏此問題,並使其更加混亂。

def quickPrint(self):
        """ prints quick glance projects details """
        if self.p_name is None:
            pass
        else:
            print 'Building ' + str(self.bldg.Value)
            print str(self.p_name.Value)
            print str(self.p_type.Value) + " -- " + str(self.p_mgr.Value)
            print str(self.start.Value) + " - " + str(self.finish.Value)

例:

class Project(object):
  def __init__(self, argument):
    self.argument = argument

  def __repr__(self):
    return str(argument)

projects = []  
for i in range(10):
  argument = i
  projects.append(Project(argument))

print projects

輸出[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

__repr__(self):定義更改為具有self.argument修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM