Python 如何正确使用队列中的popLeft进行出列?

Python 如何正确使用队列中的popLeft进行出列?,python,algorithm,stack,queue,logic,Python,Algorithm,Stack,Queue,Logic,这是一项学校作业,我不允许修改函数enqueue和dequeue以及def_u init__;(self)的代码。 我正在尝试使用popleft作为一种从队列中取出项目的方法。但是,当我使用POPLEET时,编译器返回整个队列,而不仅仅是队列的第一个堆栈。也就是说,队列的大小减少了1。对此有何解释 当我只希望编译器返回['stone','atone'],['stone','shone'],['stone','scone']]时,编译器返回['stone','atone']。就像上面的注释一样,您

这是一项学校作业,我不允许修改函数enqueue和dequeue以及def_u init__;(self)的代码。 我正在尝试使用popleft作为一种从队列中取出项目的方法。但是,当我使用POPLEET时,编译器返回整个队列,而不仅仅是队列的第一个堆栈。也就是说,队列的大小减少了1。对此有何解释


当我只希望编译器返回['stone','atone'],['stone','shone'],['stone','scone']]时,编译器返回['stone','atone']。

就像上面的注释一样,您传递的是一个
堆栈
对象,而不是堆栈中的元素。试试这个

import collections
class MyStack:
    def __init__(self):
        self._data = []

    def push(self, value):
        self._data.append(value)

    def size(self):
        #Return the number of elements in the stack
        return len(self._data)

    def toString(self):
        #Return a string representing the content of this stack
        return str(self._data)

class MyQueue:
    def __init__(self):
        self._data = collections.deque([])

    def enqueue(self, value):
        self._data.append(value)

    def dequeue(self):
        return self._data.popleft()

    def size(self):
        #return the number of elements in the queue
        return len(self._data)

queue1 = MyQueue()
dq = MyStack()
stack1 = MyStack()

stack1.push(['stone', 'atone'])
print "size of stack is now :" ,stack1.size()
queue1.enqueue(stack1)
print "size of queue is now :", queue1.size()
print "size of stack is now :" ,stack1.size()
stack1.push(['stone', 'shone'])
stack1.push(['stone', 'scone'])

dq = queue1.dequeue() # i would like dq to be ['stone','atone']

print dq.toString()

您将堆栈对象添加到队列中,而不是堆栈中的元素。这就是为什么当您
.dequeue()
返回堆栈对象时,它现在包含
[['stone','atone'],['stone','shone'],['stone','scone']
(将其推到堆栈上)。我不确定我是否完全理解你的问题,但这解释了你看到的行为。换句话说,我认为你需要在这里详细阐述一下。我怀疑您误解了任务的第一部分,您是否被要求使用堆栈填充队列?实际上,我应该按照数据结构和算法实现列表创建一个字梯游戏。我只是从我的作业中提取一些代码来问这个问题,否则这个问题会包含太多的代码。不管怎样,我只是在遵循给我的算法。现在,我需要从队列中取出第一个项目(它是一个堆栈),并能够打印出第一个项目(它是一个堆栈)。
import collections
class MyStack:
    def __init__(self):
        self._data = []

    def push(self, value):
        self._data.append(value)

    def size(self):
        #Return the number of elements in the stack
        return len(self._data)

    def toString(self):
        #Return a string representing the content of this stack
        return str(self._data)

class MyQueue:
    def __init__(self):
        self._data = collections.deque([])

    def enqueue(self, value):
        self._data.append(value)

    def dequeue(self):
        return self._data.popleft()

    def size(self):
        #return the number of elements in the queue
        return len(self._data)

queue1 = MyQueue()
dq = MyStack()
stack1 = MyStack()

stack1.push(['stone', 'atone'])
print "size of stack is now :" ,stack1.size()
queue1.enqueue(stack1.toString())
print "size of queue is now :", queue1.size()
print "size of stack is now :" ,stack1.size()
stack1.push(['stone', 'shone'])
stack1.push(['stone', 'scone'])

dq = queue1.dequeue() # i would like dq to be ['stone','atone']

print dq