所以我有一个功能:
def flip_stack(stack, nFlip):
"""flip_stack(list, num)
Edits stack to flip the first nFlip elements in the stack."""
newStack = stack[:nFlip]
for element in newStack:
stack.remove(element)
newStack.reverse()
stack = newStack + stack
return(stack)
给定
stack = [2, 3, 1, 4, 0]
nFlip = 3
stack = [1, 3, 2, 4, 0]
这其中大部分是有效的。它在正确的位置翻转堆栈。
但是,当我稍后使用它时:
difStack = stack
flip_pancakes(difStack, difStack.index(max(difStack)) + 1) # flip stack so largest number is at front
print(stack)
堆栈突然变成 [0] 有谁知道为什么?第二段代码中的flip_pancakes()函数应该只改变了difStack,对吧?
而且我意识到那个特定的部分真的很混乱。有什么办法可以改善吗?
谢谢!