我们都听说过 Python 中的数据结构,以及它们如何帮助我们进行编程和开发工作。简而言之,我们可以说数据结构是一种特殊的方式,数据可以在设备或系统中组织起来,以便以后有效地使用它。因此,在进行编程和开发相关工作时,数据结构对我们来说变得非常重要。而且,如果我们想使用数据结构,我们必须首先理解数据结构的概念&它们是如何工作的。还应该注意的是,存在的所有数据结构并不相同,它们可以是不同的类型。因此,如果我们使用两个、三个或更多的数据结构,它们可能都属于不同的类型。这些不同类型的数据结构以不同的格式排列数据,这有助于我们处理不同类型的数据。我们可以实现这些不同类型的数据结构,以便根据我们的便利性和我们正在工作的系统来安排不同的数据类型。我们可以以多种形式排列数据,例如线性形式的数据结构和其他形式。我们在编程和开发中研究的最常见的线性数据结构的例子是栈和队列。这是我们可以用来以线性形式排列数据的两种最著名的线性数据结构。可以说,栈和队列都是线性数据结构,但实际上,它们遵循不同的数据插入或移除方法。这种从给定数据结构中插入和移除数据的方法使得栈和队列彼此不同。
我们可以使用许多方法、程序、库和包来处理这些线性数据结构。许多编程语言使用不同的方法来处理这些数据结构来排列数据,但是这些数据结构的基本方法在不同的方法中是相同的。如果我们专门讨论 Python,它也是最强的编程语言之一,它为我们提供了多个包和模块,我们可以在 Python 程序中使用它们来处理这两种类型的数据结构。我们可以使用这些数据结构方法,在程序中使用 Python 的内置或基于模块的函数来处理给定的数据。queue模块就是这样的 Python 模块之一,我们可以用它来实现程序中的栈和队列数据结构方法。Python 的queue模块通过提供许多有用的功能,帮助我们在 Python 程序中使用队列和栈数据结构方法。因此,我们将在本教程中研究 Python 的这个queue模块,并与它一起学习如何使用这个模块的函数来使用队列和栈数据结构方法。
Python 中queue模块介绍
Python 以包或名为queue模块的模块的形式为我们提供了队列类函数。队列是一个 Python 模块,它为我们提供了多个内置函数,我们可以使用这些函数在 Python 程序中执行队列和栈数据结构操作。我们只需在给定的 Python 程序中导入queue模块,就可以访问或使用 Python queue模块的所有这些功能。我们可以使用queue模块的所有这些功能来执行与队列或栈数据结构相关的多个操作。我们还可以将元素存储在数据结构中,并以先进先出或后进先出的方式取出它们。
Python 的这些队列数据结构函数最初是作为许多其他面向对象编程语言(如 C、C++、C#或 Java)的类函数创建的,但后来这些函数也以模块的形式引入到 Python 中。这个队列类模块是为 Python 创建的,这样我们都可以使用这个模块的功能来处理 Python 中的队列数据结构。因此,了解这个模块以及如何在 Python 程序中使用这个模块的函数来处理队列和栈数据结构,对我们来说变得非常重要。我们将通过创建队列和栈数据结构对象,然后从这些数据结构中插入和取出元素来理解这个模块的实现。
Python 中的queue模块:安装
在最新版本的 Python 中,queue模块作为内置模块出现,这意味着该模块是在安装 Python 时预装的。因此,如果我们使用最新版本的 Python,我们不需要执行任何类型的安装过程来安装这个queue模块,我们可以直接开始使用这个模块。我们在本教程中使用的是最新版本的 Python,因此,我们不会执行该模块的任何安装过程,而是直接开始使用该模块的功能来了解它们的工作情况。在给定的 Python 程序中,我们只需要使用下面的 import 语句来使用queue模块的函数:
# Importing the queue module
import queue
因此,我们可以直接进行本模块的实现部分,了解本模块功能的实现。
Python 中的queue模块:实现
我们将通过在本部分的示例程序中使用queue模块来理解queue模块的实现,并理解该模块的功能工作。我们将使用本模块的功能执行多项操作,但我们会将它们分为以下三类,以便我们更容易理解本模块的实现部分:
- 创建先进先出队列
- 理解上溢和下溢
- 使用栈数据结构
现在,我们将在每个类别下使用一个示例程序来理解该模块不同功能的工作和功能。我们将使用queue模块的不同功能在每个类别下执行多个操作。
查看queue模块的以下分类操作实现:
1)创建先进先出模式队列:
在此类别中,我们将使用queue模块的功能执行以下三种与创建先进先出队列相关的功能:
a)创建队列对象:如果我们想使用queue模块函数创建一个 FIFO 队列,可以使用这个模块的 Queue()函数。以下是在示例程序中使用该模块的 Queue()函数创建 FIFO 队列对象的语法:
>> queue.Queue(SizeOfQueue)
如我们所见,queue模块的 queue()函数只接受一个参数,即队列的最大大小。这是此函数的强制参数,以便创建的队列对象具有固定的大小。如果我们在这个函数中提供“0”或者没有参数,这个函数将创建一个无限大小的队列对象。
(b)将元素放入队列:创建队列对象(表示队列数据结构)后,我们可以执行的下一个操作是将元素插入队列对象内部。queue模块为我们提供了 put()函数,我们可以用它将元素放入程序创建的队列对象中。以下是使用queue模块的 put()函数在队列对象中插入元素的语法:
>> QueueObject.put('element')
如我们所见,queue 模块的 put()函数也只接受一个参数,这个参数就是我们要插入到 queue 对象中的元素。
(c)从队列中获取元素:我们还将执行从该类别的队列对象中获取元素的操作。我们将使用queue模块的 get()函数以先进先出的方式从创建的队列中获取元素,但是我们还应该注意到,该函数还会从队列中移除元素,并在结果中获取元素。以下是在示例程序中使用queue模块的 get()函数的语法:
>> QueueObject.get()
如我们所见,queue模块的 get()函数不接受任何强制参数,并以 FIFO 模式从队列对象返回元素。
如上所述,为了理解这一类别下所有三个操作的实现,我们将在一个示例程序中使用这些函数。查看下面的示例程序,了解我们可以使用queue模块的功能来实现这些操作:
示例 1: 看看下面的 Python 程序,我们在其中创建并使用了一个队列对象:
# Importing the in-built queue module
import queue as qu
# Creating a queue object with maxsize argument
queueObject = qu.Queue(maxsize = 6)
# Inserting five elements in the queue of integer data type
queueObject.put(6)
queueObject.put(31)
queueObject.put(26)
queueObject.put(18)
queueObject.put(24)
# Now getting elements in the output
print('The first element inserted in the queue object created: ', queueObject.get())
print('The second element inserted in the queue object: ', queueObject.get())
print('The third element inserted in the queue object: ', queueObject.get())
print('The fourth element inserted in the queue object: ', queueObject.get())
print('The fifth element inserted in the queue object: ', queueObject.get())
输出:
The first element inserted in the queue object created: 6
The second element inserted in the queue object: 31
The third element inserted in the queue object: 26
The fourth element inserted in the queue object: 18
The fifth element inserted in the queue object: 24
我们可以看到,队列对象中的元素按照插入队列对象的顺序打印在输出中。这就是我们如何使用queue模块的这三个函数来创建一个队列对象,并在 Python 程序中使用它。
说明:首先,在上面给出的示例程序中,我们已经将 Python 的内置queue模块导入为‘qu’,这样我们就可以访问和使用该模块的功能。之后,我们使用 Queue()模块创建了一个名为“queue object”的队列对象,我们可以在这个队列对象中插入和移除元素。我们已经将队列的最大大小定义为 6,这意味着这个队列对象只能接受 6 个元素。之后,我们使用 put()函数在我们创建的队列对象中插入元素。最后,我们使用 get()函数在 FIFO 模式的输出中获取队列对象的元素。除了从队列对象中获取元素之外,get()函数还从队列对象中移除元素。
2)理解上溢和下溢
在这个实现类别中,我们将理解 Python 中与队列数据结构相关的上溢和下溢术语。顾名思义,溢出术语指的是队列对象具有最大元素时的情况,尽管如此,我们仍试图在队列中再插入一个元素。术语下溢指的是队列对象为空,但我们仍试图从中移除一个元素的情况。在这两种情况下,输出中都会显示一个错误,因此,在获取元素并将元素插入给定队列对象时,我们必须小心。除了这些操作,我们还将学习如何在输出中获取队列对象的最大大小。为了理解所有这些操作,我们将在一个示例程序中使用queue模块的功能。
查看下面的示例程序,了解我们可以使用queue模块的功能来实现这些操作:
示例 2: 看看下面的 Python 程序,我们检查了队列对象的下溢和上溢情况:
# Importing the in-built queue module
import queue as qu
# Creating a queue object with maxsize argument
quObject = qu.Queue(maxsize = 5)
# Inserting elements in the queue of integer data type
quObject.put(6)
quObject.put(31)
quObject.put(26)
quObject.put(18)
# Checking if the queue is full or not
print('Is the given queue object is full according to the maximum size of it?: ', quObject.full())
# Inserting one more element
quObject.put(24)
# Again, checking that queue is full
print('Is the given queue object is now full as its maximum size?: ', quObject.full())
# Checking size of the queue object
quSize = quObject.qsize()
print('The maximum size of the created queue object is: ', quSize)
# Now getting elements out from the queue object
print('The first element inserted in the queue object created: ', quObject.get())
print('The second element inserted in the queue object: ', quObject.get())
print('The third element inserted in the queue object: ', quObject.get())
# Checking for empty queue object
print('Is the given queue object is empty?: ', quObject.empty())
# Getting more elements out from the queue
print('The fourth element inserted in the queue object: ', quObject.get())
print('The fifth element inserted in the queue object: ', quObject.get())
# Again, Checking for empty queue object
print('Is the given queue object is empty now?: ', quObject.empty())
输出:
Is the given queue object is full according to the maximum size of it?: False
The maximum size of the created queue object is: 5
Is the given queue object is now full as its maximum size?: True
The first element inserted in the queue object created: 6
The second element inserted in the queue object: 31
The third element inserted in the queue object: 26
Is the given queue object is empty?: False
The fourth element inserted in the queue object: 18
The fifth element inserted in the queue object: 24
Is the given queue object is empty now?: True
正如我们所看到的,我们已经在程序中创建的队列对象中检查了下溢和上溢条件,这样我们就不必在执行这个程序的过程中面对任何错误。这就是我们如何使用queue模块的这些功能来处理 Python 程序中的队列数据结构,而不会出现任何错误。
说明:在程序中将queue模块导入为‘qu’后,我们创建了一个最大大小为 5 的队列对象。之后,我们在队列对象中添加了三个元素,并使用 full()函数验证队列对象是否已满。这将有助于我们防止溢出情况。之后,我们使用 put()函数在队列对象中输入了另外两个元素,并再次检查队列对象现在是否已满。然后,我们使用queue模块的 size()函数来打印队列对象的最大大小。size()函数将返回队列对象中存在的元素总数。之后,我们从队列中移除元素,并使用 get()函数将它们打印在输出中。我们还使用队列类的 empty()函数检查队列对象是否为空。该功能帮助我们检查并防止我们出现下溢情况。
3)使用栈数据结构:
到目前为止,我们已经使用了基于先进先出模式的队列数据结构,但是现在我们将使用遵循后进先出模式的队列数据结构来存储元素。后进先出模式队列数据结构通常被称为栈数据结构。栈数据结构中的所有内容保持不变,就像我们在先进先出队列数据结构中所做的一样。首先,我们必须使用queue模块的 lifoqueue()函数创建一个栈对象(LIFO queue 对象)。
下面是使用queue模块的 lifoqueue()函数定义栈数据结构对象的语法:
>> queue.LifoQueue('Size of data structure object')
我们在程序中使用 LifoQueue()函数,函数内部有一个参数。这个函数的工作原理非常类似于 Queue()函数,我们用它来定义队列数据结构。我们必须在这个函数中提供的参数是栈数据结构对象的最大大小。而且,如果我们不定义数据结构对象的大小或将其设置为“0”,那么程序将定义一个无限的栈数据结构对象。
我们将以与使用queue模块功能处理先进先出数据结构相同的方式处理栈数据结构。在一个示例程序中使用这个模块的功能,我们将对栈数据结构对象执行我们对先进先出队列对象执行的所有操作。我们还将使用queue模块的相同功能检查栈对象中的上溢和下溢情况。查看下面的示例程序,了解与栈数据结构对象相关的函数:
示例 3: 看看下面的 Python 程序,我们已经对栈数据结构执行了所有队列对象操作:
# Importing the in-built queue module
import queue as qu
# Creating a stack data structure object with maxsize argument equal to 8
stackObj = qu.LifoQueue(maxsize = 8)
# Inserting elements of integer data type in the stack object
stackObj.put(12)
stackObj.put(5)
stackObj.put(23)
stackObj.put(6)
stackObj.put(31)
# Checking if the stack object is full or not for overflow condition prevention
print('Is the given queue object is full according to the maximum size of it?: ', stackObj.full())
# Inserting some more elements inside the stack object
stackObj.put(26)
stackObj.put(18)
stackObj.put(24)
# Again, checking that if the stack object is full or not
print('Is the given stack object is now full according to its maximum size?: ', stackObj.full())
# Checking the maximum size of the stack object after Inserting all elements
stackSize = stackObj.qsize()
print('The maximum size of the stack object created in the program is: ', stackSize)
# Now getting elements out from the stack object in the LIFO manner
print('The last element inserted in the stack object: ', stackObj.get())
print('The last second element inserted in the stack object: ', stackObj.get())
print('The sixth element inserted in the stack object: ', stackObj.get())
print('The fifth element inserted in the stack object: ', stackObj.get())
# Checking that if stack object has become empty
print('Is the given stack object have become empty?: ', stackObj.empty())
# Getting more elements out from the stack object
print('The fourth element inserted in the stack object: ', stackObj.get())
print('The third element inserted in the stack object: ', stackObj.get())
print('The second element inserted in the stack object: ', stackObj.get())
print('The first element inserted in the stack object created: ', stackObj.get())
# Again Checking for the empty stack object
print('Is the given stack object in the program is empty now?: ', stackObj.empty())
输出:
Is the given queue object is full according to the maximum size of it?: False
Is the given stack object is now full according to its maximum size?: True
The maximum size of the stack object created in the program is: 8
The last element inserted in the stack object: 24
The last-second element inserted in the stack object: 18
The sixth element inserted in the stack object: 26
The fifth element inserted in the stack object: 31
Is the given stack object have become empty?: False
The fourth element inserted in the stack object: 6
The third element inserted in the stack object: 23
The second element inserted in the stack object: 5
The first element inserted in the stack object created: 12
Is the given stack object in the program is empty now?: True
正如我们所看到的,我们在队列对象上执行的所有操作都成功地在栈对象上执行,这也是我们在上面给出的示例程序中执行的。我们已经使用示例 1 和 2 中使用的相同函数执行了所有这些操作。这就是我们如何使用模块类队列的功能在 Python 程序中处理队列和栈数据结构。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/246562.html
微信扫一扫
支付宝扫一扫