从列表中获取元素并将它们与python放在一个堆栈中

时间:2014-03-22 02:54:01

标签: python list stack

这就是我现在所拥有的,并说当它试图推动我时它缺少一个参数

这是我对此代码的类

class ArrayStack:

def __init__(self):
    self._data = []

def __len__(self):
    return len(self._data)

def is_empty(self):
    return len(self._data) == 0

def push(self, a):
    self._data.append(a)

def top(self):
    if self.is_empty():
        raise Empty('Stack is empty')    
    return self._data[-1]

def pop(self):
    if self.is_empty():
        raise Empty('Stack is empty')
    return self._data.pop()

def reverselist():
    expression = input("Enter whatever: ")
    stacks = ArrayStack
    listofstuff = []
    for item in expression:
        listofstuff.append(item)
        print(listofstuff)
    for token in listofstuff:
        i = str(token)
        stacks.push(i)

1 个答案:

答案 0 :(得分:0)

您需要一个ArrayStack的实例,而不是类本身,更改为ArrayStack(),这会调用您的类的构造函数。

def reverselist():
    expression = input("Enter whatever: ")
    stacks = ArrayStack()
    listofstuff = []
    for item in expression:
        listofstuff.append(item)
        print(listofstuff)
    for token in listofstuff:
        i = str(token)
        stacks.push(i)