"移动"类中列表之间的对象

时间:2017-12-03 15:11:39

标签: python

我有一个"模拟手术"在python中编程,但不知道如何。  这是我的代码:

class Surgery:
    def __init__(self):
        self.waitingroom = []
        self.office = []

    def enter_surgery(self, p):
        self.waitingroom.append(p)

    def call_next_patient(self):
        self.office.append
        self.waitingroom.remove

    def in_treatment(self):
        if self.office == []:
            return None
        else:
            return self.office

    def treatment_done(self):
        self.office.remove

正如您所见,我希望将患者转移到#34; call_next_patient"从候车室到办公室,但随着自己"它不是全球性的。没有自我它告诉我,候车室不是defindet。我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

您可以使用popappend的组合。

来自Python docs

  

list.pop([I])   删除列表中给定位置的项目,然后将其返回。如果未指定索引,则a.pop()将删除并返回列表中的最后一项。 (方法签名中i周围的方括号表示该参数是可选的,而不是您应该在该位置键入方括号。您将在Python库参考中经常看到这种表示法。)

所以,

def call_next_patient(self):
    patient = self.office.pop()
    self.waitingroom.append(patient)

或者在一个声明中:

def call_next_patient(self):
    self.waitingroom.append(self.office.pop())

请注意,如果列表为空,则.pop会引发IndexError,因此您还需要使用try-except

def call_next_patient(self):
    try:
        self.waitingroom.append(self.office.pop())
    except IndexError:
        print('No waiting patients')