python雙向隊列deque的使用
雙向隊列deque
雙端隊列,可以快速的從另外一側(cè)追加和推出對象,deque是一個雙向鏈表,針對list連續(xù)的數(shù)據(jù)結(jié)構(gòu)插入和刪除進(jìn)行優(yōu)化。
它提供了兩端都可以操作的序列,這表示在序列的前后你都可以執(zhí)行添加或刪除操作。雙向隊列(deque)對象支持以下方法:
1. append()
添加元素x到隊列右端
###定義deque(字符串形式)
d = deque('ghi')
##或者列表形式定義deque:
# d = deque(['g', 'h', 'i'])
d.append('j')
d
deque(['g', 'h', 'i', 'j'])2. appendleft()
添加元素x到隊列左端
d.appendleft('f')
d
deque(['f', 'g', 'h', 'i', 'j'])3. clear()
移除所有元素,使其長度為0
d = deque('ghi')
d.clear()
d
deque([])4. copy()
創(chuàng)建一份淺拷貝
d = deque('xiaoweuge')
y = d.copy()
print(y)
deque(['x', 'i', 'a', 'o', 'w', 'e', 'u', 'g', 'e'])5. count()
計算 deque 中元素等于 x 的個數(shù)
d = deque('xiaoweuge-shuai')
d.count('a')
26. extend()
擴(kuò)展deque的右側(cè),通過添加iterable參數(shù)中的元素
a = deque('abc')
b = deque('cd')
a.extend(b)
a
deque(['a', 'b', 'c', 'c', 'd'])
#與append 的區(qū)別
a = deque('abc')
b = deque('cd')
a.append(b)
deque(['a', 'b', 'c', deque(['c', 'd'])])7. extendleft()
擴(kuò)展deque的左側(cè),通過添加iterable參數(shù)中的元素。注意對左添加時,在結(jié)果中iterable參數(shù)中的順序?qū)⒈环催^來添加
a = deque('abc')
b = deque('cd')
a.extendleft(b)
a
deque(['d', 'c', 'a', 'b', 'c'])8. index()
返回元素 x 在 deque 中的位置(在索引 start 之后,索引 stop 之前)。 返回第一個匹配項,如果未找到則引發(fā) ValueError
d = deque('xiaoweuge')
d.index('w')
49. insert()
在位置 i 插入 x ,如果插入會導(dǎo)致一個限長 deque 超出長度 maxlen 的話,就引發(fā)一個 IndexError
a = deque('abc')
a.insert(1,'X')
deque(['a', 'X', 'b', 'c'])10. pop()
移去并且返回deque 最右側(cè)的那一個元素。 如果沒有元素的話,就引發(fā)一個 IndexError
d = deque('abcj')
d.pop()
'j'11. popleft()
移去并且返回一個元素,deque 最左側(cè)的那一個元素。 如果沒有元素的話,就引發(fā) IndexError
d = deque('abcj')
d.popleft()
'a'12. remove(value)
移除找到的第一個 value。 如果沒有的話就引發(fā) ValueError
a = deque('abca')
a.remove('a')
a
deque(['b', 'c', 'a'])13. reverse()
將deque逆序排列,返回 None
#逆序排列
d = deque('ghi') # 創(chuàng)建一個deque
list(reversed(d))
['i', 'h', 'g']
deque(reversed(d))
deque(['i', 'h', 'g'])14. rotate(n=1)
向右循環(huán)移動 n 步。 如果 n 是負(fù)數(shù),就向左循環(huán)。如果deque不是空的,向右循環(huán)移動一步就等價于 d.appendleft(d.pop()) , 向左循環(huán)一步就等價于 d.append(d.popleft())
# 向右邊擠一擠
d = deque('ghijkl')
d.rotate(1) ? ? ? ? ? ? ? ? ? ? ?
d
deque(['l', 'g', 'h', 'i', 'j', 'k'])
# 向左邊擠一擠
d.rotate(-1) ? ? ? ? ? ? ? ? ? ??
d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
#看一個更明顯的
x = deque('12345')
x
deque(['1', '2', '3', '4', '5'])
x.rotate()
x
deque(['5', '1', '2', '3', '4'])
d = deque(['12','av','cd'])
d.rotate(1)
deque(['cd', '12', 'av']15. maxlen
Deque的最大尺寸,如果沒有限定的話就是 None
from collections import deque d=deque(maxlen=10) for i in range(20): d.append(i) d deque([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
16. 其他操作
除了以上操作,deque還支持迭代、封存、len(d)、reversed(d)、copy.deepcopy(d)、copy.copy(d)、成員檢測運算符 in 以及下標(biāo)引用例如通過 d[0] 訪問首個元素等。 索引訪問在兩端的復(fù)雜度均為 O(1) 但在中間則會低至 O(n)。 如需快速隨機訪問,請改用列表。
Deque從版本3.5開始支持 __ add (), mul __(),和 __ imul __() 操作。
from collections import deque
d = deque('ghi') ? ? ? ? ? ? ? ? # 創(chuàng)建一個deque
for elem in d:
? ? print(elem.upper())
G
H
I
#從右邊添加一個元素
d.append('j')
d ??
deque(['g', 'h', 'i', 'j'])
#從左邊添加一個元素
d.appendleft('f')
d?
deque(['f', 'g', 'h', 'i', 'j'])
#右邊刪除
d.pop() ? ? ? ? ? ? ? ? ? ? ? ? ?
'j'
#左邊邊刪除
d.popleft()
'f'
#看看還剩下啥
list(d) ? ? ? ? ? ? ? ? ? ? ? ? ?#?
['g', 'h', 'i']
#成員檢測
'h' in d ? ? ? ? ? ? ? ? ? ? ? ??
True
#添加多個元素
d.extend('jkl') ? ? ? ? ? ? ?
d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
d.clear() ? ? ? ? ? ? ? ? ? ? ? ?# empty the deque
d.pop() ? ? ? ? ? ? ? ? ? ? ? ? ?# cannot pop from an empty deque
Traceback (most recent call last):
? ? File "<pyshell#6>", line 1, in -toplevel-
? ? ? ? d.pop()
IndexError: pop from an empty deque
d.extendleft('abc') ? ? ? ? ? ? ?# extendleft() reverses the input order
d
deque(['c', 'b', 'a']參考鏈接
【萬字長文詳解】Python庫collections,讓你擊敗99%的Pythoner
到此這篇關(guān)于python雙向隊列deque的使用的文章就介紹到這了,更多相關(guān)python雙向隊列deque內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)據(jù)分析基礎(chǔ)之pandas中l(wèi)oc()與iloc()的介紹與區(qū)別介紹
我們經(jīng)常在尋找數(shù)據(jù)的某行或者某列的時常用到Pandas中的兩種方法iloc和loc,兩種方法都接收兩個參數(shù),第一個參數(shù)是行的范圍,第二個參數(shù)是列的范圍,這篇文章主要介紹了python數(shù)據(jù)分析基礎(chǔ)之pandas中l(wèi)oc()與iloc()的介紹與區(qū)別,需要的朋友可以參考下2024-07-07
Python pandas入門系列之眾數(shù)和分位數(shù)
分位數(shù)(Quantile),也稱分位點,是指將一個隨機變量的概率分布范圍分為幾個等份的數(shù)值點,分析其數(shù)據(jù)變量的趨勢,而眾數(shù)(Mode)是代表數(shù)據(jù)的一般水平,這篇文章主要給大家介紹了Python pandas系列之眾數(shù)和分位數(shù)的相關(guān)資料,需要的朋友可以參考下2021-08-08
Python?PyCharm無法打開終端命令行最終解決方案(實測成功)
這篇文章主要介紹了在使用PyCharm?2024版本時遇到的無法打開終端的問題,文中提供了兩種解決方案,大家可以根據(jù)自己的需求選擇對應(yīng)的解決方法,需要的朋友可以參考下2024-12-12
使用Python在PowerPoint演示文稿之間復(fù)制樣式
在專業(yè)演示文稿設(shè)計與制作領(lǐng)域,多場演示間保持一致性至關(guān)重要,在PowerPoint演示文稿之間復(fù)制幻燈片母版成為了一項關(guān)鍵技巧,本文中,我們將探討如何使用Python在不同的PowerPoint演示文稿之間復(fù)制幻燈片母版,提升演示文稿創(chuàng)作流程的效率與美觀度,需要的朋友可以參考下2024-05-05
python創(chuàng)建Flask Talisman應(yīng)用程序的步驟詳解
Flask是一個功能強大的Web框架,主要用于使用Python語言開發(fā)有趣的Web應(yīng)用程序,Talisman基本上是一個Flask擴(kuò)展,用于添加HTTP安全標(biāo)頭我們的Flask應(yīng)用程序易于實施,本文就給大家講講帶Talisman的Flask安全性,需要的朋友可以參考下2023-09-09

