(python)NameError: name ‘deque’ is not defined
collections模块实现栈和队列时,举了一个例子:
queueAndStack = deque()
queueAndStack.append(1)
queueAndStack.append(2)
queueAndStack.append("hello")
print(list(queueAndStack))
#实现队列功能,从队列中取一个元素,根据先进先出原则,这里应输出 1
print(queueAndStack.popleft())
#实现栈功能,从栈里取一个元素,根据后进先出原则,这里应输出 hello
print(queueAndStack.pop())
#再次打印列表
print(list(queueAndStack))
运行以后出现的错误提示:
解决:
在第一行加入语句from collections import deque后成功运行。
运行结果: