从列表中弹出项目 - 错误“对象不可编写脚本”

时间:2015-05-13 11:14:12

标签: python list pop

我有以下功能,其中的想法是使用vip_push添加到堆栈中的项目将在使用push添加项目之前弹出。除此之外,VIPStack仍然删除最后添加的项目,换句话说:

推动将采取 x,任何类型 并返回无

vip_push将采取 x,任何类型 并返回无

pop将不带任何参数,并按照上面的指定从堆栈中返回一个项目。

但是,我收到一条错误,“builtin_function_or_method”对象不能编写脚本。如果我只想尝试从列表中弹出一些东西,我不明白这是怎么回事......

CODE:

class VIPStack:

    # Put your VIPStack data members here
    def __init__(self):
        self.vip_list=[]
        self.push_list=[]


    def push(self, x):
        self.push_list.insert(0,x)
        return None

    def vip_push(self, x):
        self.vip_list.insert(0,x)
        return None


    def pop(self):
        if len (self.vip_list)>=1:
            number=self.vip_list.pop[0]
            return number
        else:
            if len (self.push_list)>=1:
                number2=self.push_list.pop[0]
                return number2
            else:
                print("No numbers have been pushed")




def main():

    v = VIPStack()

    # The outputs are None but the stack should obviously get updated
    print("v.vip_push(1) =", v.vip_push(1))
    print("v.vip_push(2) =", v.vip_push(2))
    print("v.push(3) =", v.push(3))
    print("v.push(4) =", v.push(4))

    # The correct order in which the elements are popped is [2,1,4,3]
    # Make sure to actually remove the element from the stack too
    print("v.pop() =", v.pop())
    print("v.pop() =", v.pop())
    print("v.pop() =", v.pop())
    print("v.pop() =", v.pop())

if __name__ == '__main__':
    main()

这是代码的预期输出:

> python Q3.py
v.vip_push(1) = None
v.vip_push(2) = None
v.push(3) = None
v.push(4) = None
v.pop() = 2
v.pop() = 1
v.pop() = 4
v.pop() = 3

2 个答案:

答案 0 :(得分:0)

你的pop方法调用有方括号

.icons {display: table; margin: 0 auto;}

应该是:

number=self.vip_list.pop[0]

同样对于另一行开始编号2

答案 1 :(得分:0)

正确的语法是

pop[0]

而不是

((a == 3 || a == 5 || a == 7) && (b == 2) && (c == 5 || c == 6) && (d == 8 || d == 9 || d == 0))