Python deque

隊列是一個核心庫,允許用戶根據先進先出 ( 先進先出)原則定義列表。相比之下,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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
L42C2的頭像L42C2
上一篇 2024-10-03 23:16
下一篇 2024-10-03 23:16

相關推薦

  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29

發表回復

登錄後才能評論