从python中的列表中删除对象实例

时间:2013-04-25 00:36:32

标签: python list object python-2.7 del

我有这个电梯任务,我被困在一个地方。我有一个构建对象,其中包含一个客户列表。我正在尝试一种方法让客户进入电梯 - 客户被附加到电梯列表中,但无法找到从建筑物列表中删除该客户的方法:尝试使用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

1 个答案:

答案 0 :(得分:0)

嗨,这是我的一些建议:

1)建筑物应该有N层,你可以有一个建筑工厂来建造有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()