我們都聽說過 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/zh-tw/n/246562.html