【发布时间】:2022-11-01 21:45:25
【问题描述】:
if __name__ == '__main__':
N = int(input())
lst = []
nums = []
for i in range(N):
a = input()
temp = a.split()
if 'insert' in temp:
lst.insert(int(temp[1]), int(temp[2]))
elif 'print' in temp:
nums.append(lst)
elif 'remove' in temp:
del lst[lst.index(int(temp[1]))]
elif 'append' in temp:
lst.append(int(temp[1]))
elif 'sort' in temp:
lst.sort()
elif 'pop' in temp:
lst.pop(-1)
elif 'reverse' in temp:
lst = lst.reverse()
for i in nums:
print(i)
输入
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
输出
[9, 5, 1]
[9, 5, 1]
None
预期产出
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
我在 HackerRank 上做任务,我几乎已经完成了,但是突然在“for i in range(N)”程序的每个循环中添加最后一个列表三次独立于输入。
我已经尝试了很多调试测试,但在我的脚本中找不到错误。
【问题讨论】:
-
在第一个 for 循环的末尾添加
print(lst)和print(nums),你就会看到发生了什么。当您编辑lst时,nums会随之改变
标签: python