【发布时间】:2013-08-05 04:25:04
【问题描述】:
所以我有一个函数:
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)
stack突然变成[0] 有谁知道为什么?第二段代码中的flip_pancakes()函数应该只改变了difStack吧?
而且我意识到那个特定的部分真的很混乱;有什么办法可以改善吗?
谢谢!
【问题讨论】:
标签: list python-3.x