【发布时间】:2020-03-11 02:33:38
【问题描述】:
我正在尝试在 python 中实现一个堆栈,并且正在尝试使用 list 数据结构。如果我想使用 pop 方法通过使用 现有数组 中的元素来“填充”一个空数组,我该怎么办?
# Implementing Stacks in Python through the given list structure
practiceStack = []
practiceStack.append(['HyunSoo', 'Shah'])
practiceStack.append('Jack')
practiceStack.append('Queen')
practiceStack.append(('Aces'))
# printing every element in the list/array
for i in practiceStack:
print(i)
# since stacks are LIFO (last in first out) or FILO (first in last out), the pop method will remove the first thing we did
emptyArrayPop = []
这是我尝试过的(通过使用 for 循环)并不断收到 use integers not list 错误
for i in practiceStack:
emptyArrayPop[i].append(practiceStack.pop)
print(emptyArrayPop)
【问题讨论】:
-
你的意思是
practiceStack.pop()? -
没有这样的错误信息。