python队列
In our previous tutorial we have discussed about python time sleep function of the time module. In this tutorial we will learn about python queue module.
在之前的教程中,我们讨论了有关time模块的python time sleep函数。 在本教程中,我们将学习python队列模块。
Python队列 (Python queue)
When we want to process our data in the way they have arrived, then using queue is the best option. Queue is a concept of first in first out data structure. Have a look at the following example:
当我们想以数据到达的方式处理数据时,最好使用队列。 队列是先进先出数据结构的概念。 看下面的例子:
# importing the queue module
import queue
# taking an object of Queue()
q = queue.Queue()
# enqueueing some value in the object of Queue
q.put('A')
q.put('B')
q.put('C')
q.put('D')
q.put('E')
# retrieving the values of the Queue
for i in range(q.qsize()):
print(q.get())
This will output:
![python queue](https://i-blog.csdnimg.cn/blog_migrate/f1616f404730f3900277e06f52c10162.png)
这将输出:
Python队列示例代码分析 (Python Queue Example code analysis)
In the very first line we have imported the python queue module. Then we have created an object of Queue as the queue module contain three classes like Queue
, LifoQueue
and PriorityQueue
.
在第一行中,我们导入了python队列模块。 然后,我们创建了一个Queue对象,因为该队列模块包含三个类,例如Queue
, LifoQueue
和PriorityQueue
。
Then we have added some value to the object of queue using put() method. Finally we have retrieved the values until the size of the q, using get() method. And if you notice the output, you will see that the outputs are as the way we have added.
然后,我们使用put()方法向队列对象添加了一些值。 最后,我们使用get()方法检索了值直到q的大小。 而且,如果您注意到输出,您将看到输出与我们添加的方式相同。
Python Queue常用方法 (Python Queue common methods)
In the python queue module, the followings are the most common methods that are used to manipulate or operate on the queue objects. We have already seen three methods in the previous example. They are put()
, get()
and qsize()
. We will see some other methods.
在python队列模块中,以下是用于操作队列对象或对其进行操作的最常用方法。 在前面的示例中,我们已经看到了三种方法。 它们是put()
, get()
和qsize()
。 我们将看到其他一些方法。
Python队列full()函数 (Python Queue full() function)
To check whether a queue object is full or not. It returns true if the queue is full otherwise false.
检查队列对象是否已满。 如果队列已满,则返回true,否则返回false。
Python Queue empty()函数 (Python Queue empty() function)
If the queue is empty then it returns true otherwise false.
如果队列为空,则返回true,否则返回false。
See the following example:
请参见以下示例:
# importing only the Queue from the queue module
from queue import Queue
# taking an object of Queue()
q = Queue()
print("Initially the size of queue is %s" % q.qsize())
print("Checking whether queue is empty or not. Empty?? = %s" % q.empty())
# enqueueing some value in the object of Queue
q.put('A')
q.put('B')
q.put('C')
q.put('D')
q.put('E')
print("After adding some value, size of queue is %s" % q.qsize())
print("Checking whether queue is full or not. Full?? = %s" % q.full())
# retrieving the values of the Queue
for i in range(q.qsize()):
print("Retrieved = ", end=' ')
print(q.get())
# after retrieving, check the size of the object
print("Size of queue is = %s " % q.qsize())
Above python queue example code will produce following output.
上面的python队列示例代码将产生以下输出。
Initially the size of queue is 0
Checking whether queue is empty or not. Empty?? = True
After adding some value, size of queue is 5
Checking whether queue is full or not. Full?? = False
Retrieved = A
Retrieved = B
Retrieved = C
Retrieved = D
Retrieved = E
Size of queue is = 0
Python LifoQueue (Python LifoQueue)
Python LifeQueue is similar to queue except that it’s last-in-first-out data structure. Below is a simple python LifoQueue example code.
Python LifeQueue与队列相似,除了它的后进先出数据结构。 以下是一个简单的python LifoQueue示例代码。
import queue
q = queue.LifoQueue()
q.put('A')
q.put('B')
q.put('C')
for i in range(q.qsize()):
print(q.get())
Below image shows the output produced by python LifoQueue example code.
下图显示了python LifoQueue示例代码产生的输出。
Python优先级队列 (Python Priority Queue)
Priority Queue returns the item in the order of the priority. So when we add an item to the priority queue, we also provide it’s priority. Then we retrieve items, they are returned in the order of the priority. Lower number priority items are returned first.
Priority Queue按优先级顺序返回项目。 因此,当我们将项目添加到优先级队列时,我们还提供了它的优先级。 然后我们检索项目,它们按优先级顺序返回。 优先级较低的项目将首先返回。
Below is python PriorityQueue example code.
以下是python PriorityQueue示例代码。
import queue
q = queue.PriorityQueue()
q.put((1, 'A'))
q.put((3, 'B'))
q.put((2, 'C'))
for i in range(q.qsize()):
print(q.get())
Here is the output produced by python priority queue example program.
这是python优先级队列示例程序产生的输出。
优先级队列Python heapq模块 (Priority Queue Python heapq module)
Python Queue module PriorityQueue
functions are synchronized. There is another module heapq
that also implements priority queue in python.
Python Queue模块PriorityQueue
函数已同步。 还有另一个模块heapq
,也可以在python中实现优先级队列。
import heapq
q = []
heapq.heappush(q, (2, 'B'))
heapq.heappush(q, (1, 'A'))
heapq.heappush(q, (3, 'C'))
while q:
item = heapq.heappop(q)
print(item)
Output produced by heapq priority queue implementation example program is shown in below image.
下图显示了heapq优先级队列实现示例程序产生的输出。
That’s all about Python Queue, Priority Queue and LifoQueue.
这就是关于Python队列,优先级队列和LifoQueue的全部内容。
Reference: Official Doc
参考: 官方文件
python队列