x = []
p = []
n = int(input())
for _ in range(n):
cmd = input().split()
if cmd[0] == "insert":
x.insert(int(cmd[1]), int(cmd[2]))
elif cmd[0] == "print":
p.append(x)
elif cmd[0] == "remove":
x.remove(int(cmd[1]))
elif cmd[0] == "append":
x.append(int(cmd[1]))
elif cmd[0] == "sort":
x.sort()
elif cmd[0] == "pop":
x.pop()
elif cmd[0] == "reverse":
x.reverse()
for i in p:
print(i)
在python 3.7中,用于输入:
> 12
> insert 0 5
> insert 1 10
> insert 0 6
> print
> remove 6
> append 9
> append 1
> sort
> print
> pop
> reverse
> print
应该给出输出:
[6,5,10][1、5、9、10][9,5,1]
但是,它正在给予:
[[9,5,1] [9,5,1] [9,5,1]
请在代码中找到错误
append
不进行复制。您想要类似的东西>
p.append(x[:])
将
x
的“快照”添加到列表中。