【发布时间】:2021-10-31 00:11:22
【问题描述】:
我写了一个代码来执行多个命令。找不到原因,当我输入pop,并打印deque对象时,最后一个元素没有被删除,谢谢你的帮助!
from collections import deque
N=int(input())
s=deque()
for i in range(N):
inp=input().split()
if len(inp)==1:
comand =inp[0]
else :
comand=inp[0]
element=inp[1]
if comand=='append':
s.append(element)
elif comand=='appendleft':
s.appendleft(element)
elif comand=='pop':
s.pop()
else:
s.popleft()
for i in s:
print(i)
【问题讨论】:
-
您能否提供一系列命令来重现您声称看到的行为?
pop对我来说很好。 -
2 append 50 pop deque(['50'])
-
对,我忘了我用
pop 2测试过,以为命令必须有一个虚拟的第二个元素。但这会导致问题:你的缩进。
标签: python list collections coding-style deque