亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python 队列:FIFO、LIFO 示例

什么是 Python 队列?

队列是保存数据的容器。首先输入的数据将首先被删除,因此队列也称为“先进先出”(FIFO)。队列有前后两端。物品从后面进入,从前面取出。

在本 Python 教程中,您将学习:

Python 队列是如何工作的?

队列可以很容易地与现实世界的示例进行比较,即在售票柜台排队等候的人,首先站立的人将首先获得票,然后是下一个人,依此类推。队列数据结构也是如此。

这是队列的图解表示:

后部 表示项目在队列中插入的点。在这个例子中,7 是它的值。

正面 表示将删除队列中的项目的点。如果你从队列中移除一个元素,你将得到的第一个元素是 1,如图所示。

项目 1 是第一个插入队列的项目,而在删除它时,它是第一个出来的项目。因此该队列称为FIRST IN FIRST OUT(FIFO)

在队列中,项目按顺序移除,不能从中间移除。您只是不能从队列中随机删除第 5 项,为此您必须删除 5 之前的所有项目。队列中的项目将按照插入的顺序删除。

Python 中的队列类型

Python中的队列主要有两种:

Python队列安装

在 python 中使用队列非常容易。以下是在代码中使用队列的步骤。

步骤 1) 你只需要导入队列模块,如下图:

import queue

默认情况下,该模块在 python 中可用,您不需要任何额外的安装即可开始使用队列。队列有FIFO(先进先出)和LIFO(后进先出)2种。

步骤 2) 要使用 FIFO 队列,请使用导入的队列模块调用 Queue 类,如下所示:

import queue
q1 = queue.Queue()

步骤 3) 要使用 LIFO 队列,请调用 LifoQueue() 类,如下所示:

import queue
q1 = queue.LifoQueue()

Queue 和 LifoQueue 类中可用的方法

以下是 Queue 和 LifoQueue 类中可用的重要方法:

先进先出队列示例

在先进先出的情况下,先走的元素会最先出来。

在队列中添加和项目

让我们处理一个在队列中添加项目的示例。要开始使用队列,首先导入模块队列,如下例所示。

要添加项目,您可以使用 put() 方法,如示例所示:

import queue
q1 = queue.Queue()
q1.put(10) #this will additem 10 to the queue.

默认情况下,队列的大小是无限的,您可以向其中添加任意数量的项目。如果您想定义队列的大小,可以按以下方式进行操作

import queue
q1 = queue.Queue(5) #The max size is 5.
q1.put(1)
q1.put(2)
q1.put(3)
q1.put(4)
q1.put(5)
print(q1.full()) # will return true.

输出:

True

现在队列的大小是 5,它不会超过 5 个项目,方法 q1.full() 将返回 true。添加更多项将不再执行代码。

从队列中删除一个项目

要从队列中删除一个项目,您可以使用名为 get() 的方法。该方法在调用时允许队列中的项目。

下面的例子展示了如何从队列中移除一个项目。

import queue
q1 = queue.Queue()
q1.put(10)

item1 = q1.get()

print('The item removed from the queue is ', item1)

输出:

The item removed from the queue is  10

后进先出队列示例

在先出队列最后的情况下,最后进入的元素将最先出来。

要使用 LIFO,即后进先出队列,我们​​需要导入队列模块并使用 LifoQueue() 方法。

在队列中添加和项目

在这里我们将了解如何将一个项目添加到 LIFO 队列中。

import queue
q1 = queue.LifoQueue()
q1.put(10)

您必须在 LifoQueue 上使用 put() 方法,如上例所示。

从队列中删除一个项目

要从 LIFOqueue 中删除项目,您可以使用 get() 方法。

import queue
q1 = queue.LifoQueue()
q1.put(10)

item1 = q1.get()

print('The item removed from the LIFO queue is ', item1)

输出:

The item removed from the LIFO queue is  10

在队列中添加超过 1 个项目

在上面的示例中,我们已经看到了如何为 FIFO 和 LIFOqueue 添加单个项目并删除该项目。现在我们将了解如何添加多个项目并将其删除。

在 FIFO 队列中添加和项

import queue
q1 = queue.Queue()

for i in range(20):
    q1.put(i) # this will additem from 0 to 20 to the queue

从 FIFO 队列中删除一个项目

import queue
q1 = queue.Queue()

for i in range(20):
    q1.put(i) # this will additem from 0 to 20 to the queue

while not q1.empty():
print("The value is ", q1.get()) # get() will remove the item from the queue.

输出:

The value is  0
The value is  1
The value is  2
The value is  3
The value is  4
The value is  5
The value is  6
The value is  7
The value is  8
The value is  9
The value is  10
The value is  11
The value is  12
The value is  13
The value is  14
The value is  15
The value is  16
The value is  17
The value is  18
The value is  19

在 LIFOqueue 中添加和项目

import queue
q1 = queue.LifoQueue()
for i in range(20):
    q1.put(i) # this will additem from 0 to 20 to the queue

从 LIFOqueue 中删除一个项目

import queue
q1 = queue.LifoQueue()

for i in range(20):
    q1.put(i) # this will additem from 0 to 20 to the queue


while not q1.empty():
print("The value is ", q1.get()) # get() will remove the item from the queue. 

输出:

The value is  19
The value is  18
The value is  17
The value is  16
The value is  15
The value is  14
The value is  13
The value is  12
The value is  11
The value is  10
The value is  9
The value is  8
The value is  7
The value is  6
The value is  5
The value is  4
The value is  3
The value is  2
The value is  1
The value is  0 

排序队列

下面的例子展示了队列排序。用于排序的算法是冒泡排序。

import queue
q1 = queue.Queue()
#Addingitems to the queue
q1.put(11)
q1.put(5)
q1.put(4)
q1.put(21)
q1.put(3)
q1.put(10)

#using bubble sort on the queue
n =  q1.qsize()
for i in range(n):
    x = q1.get() # the element is removed
    for j in range(n-1):
        y = q1.get() # the element is removed
        if x > y :  
            q1.put(y)   #the smaller one is put at the start of the queue
        else:
            q1.put(x)  # the smaller one is put at the start of the queue
            x = y     # the greater one is replaced with x and compared again with nextelement
    q1.put(x)

while (q1.empty() == False): 
print(q1.queue[0], end = " ")  
        q1.get()

输出:

3 4 5 10 11 21

反转队列

要反转队列,可以使用另一个队列和递归。

下面的例子展示了如何让队列倒转。

示例:

import queue
q1 = queue.Queue()

q1.put(11)
q1.put(5)
q1.put(4)
q1.put(21)
q1.put(3)
q1.put(10)

def reverseQueue (q1src, q2dest) :  
    buffer = q1src.get()
    if (q1src.empty() == False) :
reverseQueue(q1src, q2dest)      #using recursion
    q2dest.put(buffer)
return q2dest

q2dest = queue.Queue()
qReversed = reverseQueue(q1,q2dest)

while (qReversed.empty() == False): 
print(qReversed.queue[0], end = " ")  
        qReversed.get()

输出:

10 3 21 4 5 11

总结:


Python

  1. Python 运算符
  2. Python 列表
  3. Python 元组
  4. Python字典
  5. Python String strip() 函数与示例
  6. Python 字符串长度 | len() 方法示例
  7. Python 教程中的收益:生成器和收益与返回示例
  8. 集合中的 Python 计数器示例
  9. Python 中的 Enumerate() 函数:循环、元组、字符串(示例)
  10. Python time.sleep():为您的代码添加延迟(示例)
  11. Python 列表 index() 与示例
  12. FIFO vs LIFO:差异、优缺点、应用等