简体   繁体   English

从列表中获取元素,并使用python将其放入堆栈中

[英]Taking elements from list and putting them in a stack with python

This is what i currently have and says that it is missing one argument when it tries to push i 这是我目前拥有的内容,并说它在试图推动我时缺少一个参数

this is the class that I have for this code 这是我为此代码提供的课程

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)

You need an instance of ArrayStack , not the class itself, change for ArrayStack() , this calls the constructor of your class. 您需要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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python从和数组中获取元素并将其放入2D数组中 - Python taking elements from and array and putting them into a 2d array 从DF中获取特定元素并将其放入自己的DF中。 蟒蛇 - taking specific elements from a DF and putting them into own DF. Python 获取对象的JSON列表并将其放入python列表 - Taking JSON list of objects and putting them into a python list 将事物从一个列表中取出并放入另一个列表中(python) - Taking things out of one list and putting them in another (python) 从CSV文件中提取项目并将元素放入列表中 - Taking items from a CSV file and putting the elements in a list 从 csv 文件中取出一列并将其放入 python 的列表中 - Taking a column from a csv file and putting it into a list in python 如果我将多个元素附加到 python 中的列表中,是否更值得将它们全部放入列表然后扩展它? - If I am appending multiple elements to a list in python, is it more worth putting them all into a list then extending it? 从列表中取出元素 - Taking Elements From a List 分隔/解析某些元素的列表,然后将它们放在单独的列表中。 (蟒蛇) - Separating/Parsing a list for certain elements and putting them in separate lists. (Python) 一次从 1d 列表中取出 n 个元素并将它们添加到 2d 列表 - Taking n elements at a time from 1d list and add them to 2d list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM