隊列是一個核心庫,允許用戶根據先進先出 ( 先進先出)原則定義列表。相比之下,Python 中的 Deque 擁有相反的原理: LIFO(後進先出)隊列。在下面的教程中,我們將只通過一些例子來了解 Python 中的 Deque 是什麼。
那麼,讓我們開始吧。
理解 Python 中的「得」字
一個德清,也稱為一個雙端隊列,具有從任意一端插入和刪除數據元素的屬性。德格模塊是圖書館的一部分,被稱為收藏。它包含添加和移除數據元素的屬性,這些數據元素可以通過參數直接調用。為了申報一個德格,我們必須先導入收藏庫。
讓我們考慮以下語法來理解 Python 中的 deque 模塊是如何工作的。
語法:
# importing the deque module
# from the collections library
from collections import deque
# declaring the deque
list_name = deque()
說明:
在上面的代碼片段中,我們已經從藏書庫中導入了德格模塊,並通過將列表的名稱,即上面案例中的列表 _ 名稱分配給德格()模塊來聲明德格。這裡我們還可以觀察到,為了實現這些內置方法,我們不需要任何類。它們可以直接實現。
讓我們考慮一個基於de quee模塊的簡單例子。
示例:
# importing the deque module
# from the collections library
from collections import deque
# declaring the deque
fruit_list = deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])
# printing the deque
print(fruit_list)
輸出:
deque(['Apple', 'Mango', 'Peaches', 'Banana', 'Papaya'])
說明:
在上面的例子中,我們已經從收藏庫中導入了德格模塊。然後,我們使用德格模塊將水果列表定義為德格,指定了一些水果名稱。然後,我們為用戶列印了聲明的德奎。結果,成功列印出包含一串水果名稱的已聲明的德格。
現在,讓我們了解一下德客上的各種操作。
德格上的一些運算
有各種各樣的操作可以在德格使用。下面列出了其中一些及其描述:
| 南號碼 | 操作 | 描述 |
| one | 追加() | append()函數用於將參數中的數據元素添加到 deque 的右端。 |
| Two | appendleft() | appendleft()函數用於將參數中的數據元素添加到 deque 的左端。 |
| three | 流行音樂() | pop()函數用於從 deque 的右端刪除數據元素。 |
| four | popleft() | popleft()函數用於從 deque 的右端刪除數據元素。 |
| five | 索引(元素、開始、結束) | index()函數用於返回參數中指定的第一個索引值,從開始到結束索引開始搜索。 |
| six | 插入(I,x) | insert()函數用於在參數中提到的索引號「I」處插入參數「x」中描述的值。 |
| seven | 移除() | remove()函數用於刪除參數中指定值的第一次出現。 |
| eight | 計數() | count()函數用於計算參數中指定值的總出現次數。 |
| nine | 擴展(可迭代) | extend()函數用於在 deque 的右端插入多個數據元素。傳遞的參數是可迭代的。 |
| Ten | extendleft(可迭代) | extendleft()函數用於在 deque 的左端插入多個數據元素。傳遞的參數是可迭代的。作為左追加的輸出,順序也是相反的。 |
| Eleven | 反向() | reverse()函數用於反轉數據元素的順序。 |
| Twelve | 旋轉() | rotate()函數用於將 deque 旋轉參數中提到的數值。如果提到的數字是負值,那麼旋轉發生在左邊。否則旋轉向右。 |
現在讓我們考慮一些基於de quee模塊的例子。
示例:
# importing the collections library
# for deque operations
import collections
# declaring the deque
my_deque = collections.deque([10, 20, 30, 40, 50])
# using the append() function to add
# data element at right end
# inserting 60 at the end of the deque
my_deque.append(60)
# printing the resultant deque
print( "The deque after appending at right: " )
print( my_deque )
# using the appendleft() function to add
# data element at left end
# inserting 70 at the starting of the deque
my_deque.appendleft(70)
# printing the resultant deque
print( "The deque after appending at left: " )
print( my_deque )
# using the pop() function to remove
# data element from the right end
# removing 60 from the right end of deque
my_deque.pop()
# printing the resultant deque
print( "The deque after removing from right: " )
print( my_deque )
# using the popleft() function to remove
# data element from the left end
# removing 70 from the left end of deque
my_deque.popleft()
# printing the resultant deque
print("The deque after removing from left: " )
print( my_deque )
輸出:
The deque after appending at right:
deque([10, 20, 30, 40, 50, 60])
The deque after appending at left:
deque([70, 10, 20, 30, 40, 50, 60])
The deque after removing from right:
deque([70, 10, 20, 30, 40, 50])
The deque after removing from left:
deque([10, 20, 30, 40, 50])
說明:
在上面的代碼片段中,我們已經導入了集合庫,並聲明了一個 deque。然後我們使用了像 append() 和 appendleft() 這樣的操作,以便將一些數據元素插入到 deque 的兩端,並為用戶列印修改後的 deque。類似地,我們使用了 pop() 和 popleft() 等操作,以便從 deque 的兩端移除數據元素,並為用戶列印結果 deque。
示例:
# importing the collections library
import collections
# declaring the deque
my_deque = collections.deque(['Jan', 'Feb', 'Mar', 'Mar', 'Feb', 'April', 'Feb'])
# using the index() function to print
# the first occurrence of data element: Feb
print( "The first occurs of 'Feb' at a position: " )
print( my_deque.index('Feb', 2, 7) )
# using the insert() function to insert
# the data element 'Jan' at 4th position
my_deque.insert(3,'Jan')
# printing the resultant deque
print( "The deque after inserting 'Jan' at 4th position: " )
print( my_deque )
# using the count() function to count
# the occurrences of data element 'Feb'
print( "The count of 'Feb' in deque: " )
print( my_deque.count('Feb') )
# using the remove() function to remove
# the first occurrence of data element 'Mar'
my_deque.remove('Mar')
# printing the resultant deque
print( "The deque after removing the first occurrence of 'Mar': " )
print( my_deque )
輸出:
The first occurs of 'Feb' at a position:
4
The deque after inserting 'Jan' at 4th position:
deque(['Jan', 'Feb', 'Mar', 'Jan', 'Mar', 'Feb', 'April', 'Feb'])
The count of 'Feb' in deque:
3
The deque after removing the first occurrence of 'Mar':
deque(['Jan', 'Feb', 'Jan', 'Mar', 'Feb', 'April', 'Feb'])
說明:
在上面的代碼片段中,我們再次導入了集合庫,並聲明了一個 deque。然後,我們使用了索引()操作來分別檢查索引號 2 和 7 之間的數據元素【2 月】的首次出現。然後我們使用了 insert() 操作,以便在第 4 個位置插入數據元素『Jan』。然後,我們使用 count() 操作來計算數據元素「2 月」在 deque 中的出現次數。最後,我們使用了 remove() 操作,以便將數據元素『Mar』的第一次出現從 deque 中刪除,並為用戶列印結果 deque。
原創文章,作者:L42C2,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127695.html