【问题标题】:Removing an object instance from a list in python从python中的列表中删除对象实例
【发布时间】:2013-04-25 00:36:32
【问题描述】:

我有这个电梯任务,但我被困在一个地方。我有一个包含客户列表的建筑对象。我正在尝试一种让客户进入电梯的方法 - 客户被附加到电梯列表中,但无法找到从建筑物列表中删除该客户的方法:尝试使用 dellist.remove(item) 但没有乐趣。谁能指出我正确的方向吗?

class building(object):  
    def __init__(self, floors = 0, customerNo = 0,):
        self.floors = 0
        self.myE = elevator()
        self.customerNo = 0
        self.customersWaiting = []
        self.customersTransported = []

    def initCustomers(self):        
        cnt = 1
        cust = cnt
        while cnt <= myB.customerNo :
            floor = random.randint(0, self.floors - 1)
            destination = random.randint(0, self.floors - 1)
            cust = customer(cnt, floor , destination )
            self.customersWaiting.append(cust)
            cnt +=1

class customer(object):
    def __init__(self,cnt, floor = 0, destination = 0):
        self.cnt = cnt
        self.floor = floor
        self.destination = destination

    def enterElevator(self):
        for cust in myB.customersWaiting :
            if myB.myE.lvl == self.floor :
                myB.myE.customersIn.append(self)
                #del cust(self)
                #myB.customersWaiting.remove(self)
            else:
                pass

【问题讨论】:

    标签: python list object python-2.7 del


    【解决方案1】:

    您好,这是我的一些建议:

    1) 建筑物应该有 N 层,你可以有一个 buildingFactory 来创建有 n 层的建筑物。

    def createBuilding(self, N):
        return building = Building(N)
    class Building:
        def __init__(self, N):
            self.floorWatingList= []
            for f in range(N):
                self.floorWaitingList.append() = []
    

    2) 使用 clear() 或 pop() 方法从列表中删除客户

    for c in building.floorWaitingList[current]:
          elvator.enter(c)
    building.floorWaitingList.clear()
    

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 2014-06-29
      • 2023-04-03
      • 2020-11-07
      • 2021-08-03
      相关资源
      最近更新 更多