在堆栈中弹出元素之前打印值[python]
假设我的堆栈s包含:在堆栈中弹出元素之前打印值[python],python,Python,假设我的堆栈s包含: A B C 当我弹出堆栈时,如果我打印堆栈,我只想在堆栈中弹出的元素之前打印元素,可以这样做吗?在这种情况下,C被弹出,因此它将只打印B,而不是同时打印B和A 例如: 如果userinput是“undo”,我要做的是弹出我添加到堆栈中的最新元素,并打印之前的元素 userInput = input("Enter option:") if userInput == 'undo': L.pop() print(L) #is it possible for s
A
B
C
当我弹出堆栈时,如果我打印堆栈,我只想在堆栈中弹出的元素之前打印元素,可以这样做吗?在这种情况下,C被弹出,因此它将只打印B,而不是同时打印B和A
例如:
如果userinput是“undo”,我要做的是弹出我添加到堆栈中的最新元素,并打印之前的元素
userInput = input("Enter option:")
if userInput == 'undo':
L.pop()
print(L) #is it possible for something like (L-1)??
你可以这样做:
userInput = input("Enter option:")
if userInput == 'undo':
L.pop()
print(L[-1])
这将通过“弹出”撤消最后一个添加,然后打印列表/堆栈中当前的最后一项。如果L是列表,您可以使用
L[-1]
打印其最后一个元素。打印L[-1]
?