1. 程式人生 > >5.3.3 deque對象

5.3.3 deque對象

計算 mil 註意 app xtend 索引 true rotate art

class collections.deque([iterable[, maxlen]])

返回一個新雙向隊列,當有輸入叠代器時。會從左至右地加入到隊列裏。假設沒有輸入參數,就創建一個空隊列。

deque是一個具有棧和隊列特性的數據結構。它支持線程安全、內存優化和兩端彈出、插入元素,不管從那一個方向彈出元素都是O1)的時間花費。在內置的數據類型list也支持相關的操作。可是它設計為對固定元素進行操作,假設插入和彈出一個元素。它的內存操作時間花費是o(n)。假設參數maxlen沒有指定,或者指定為None,它的長度是隨意的,假設有指定長度。就不能加入元素超過指定長度。假設隊列已經達到指定長度,從一端加入一個元素。就會從還有一端彈出一個元素,從而保持元素不變。

因而它很適合跟蹤最後活動對象的場合。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

print(dq)

結果輸出例如以下:

deque([1], maxlen=5)

deque主要支持下面方法:

append(x)

加入元素x到隊列的右邊。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

結果輸出例如以下:

deque([1, 2, 3], maxlen=5)

appendleft(x)

加入元素x到隊列左邊。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.appendleft(1)

dq.appendleft(2)

dq.appendleft(3)

print(dq)

結果輸出例如以下:

deque([3, 2, 1], maxlen=5)

clear()

清除隊列裏全部元素。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.appendleft(1)

dq.appendleft(2)

dq.appendleft(3)

print(dq)

dq.clear()

print(dq)

結果輸出例如以下:

deque([3, 2, 1], maxlen=5)

deque([], maxlen=5)

count(x)

計算隊列元素的個數是否等於x個,假設大於或等於都返回True。否則返回False

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

print(dq.count(1))

print(dq.count(2))

print(dq.count(3))

print(dq.count(6))

結果輸出例如以下:

deque([1, 2, 3], maxlen=5)

1

1

1

0

extend(iterable)

從右邊擴展隊列。參數是叠代器對象。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

dq.extend([7, 8, 9])

print(dq)

結果輸出例如以下:

deque([1, 2, 3], maxlen=5)

deque([2, 3, 7, 8, 9], maxlen=5)

extendleft(iterable)

從左邊加入叠代對象裏的元素。註意加入順序與叠代對象順序剛好相反。

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

dq.extendleft([7, 8, 9])

print(dq)

結果輸出例如以下:

deque([1, 2, 3], maxlen=5)

deque([9, 8, 7, 1, 2], maxlen=5)

pop()

從右邊刪除一個元素。而且把這個元素返回。假設隊列為空,拋出異常IndexError

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

dq.pop()

print(dq)

輸出結果例如以下:

deque([1, 2, 3], maxlen=5)

deque([1, 2], maxlen=5)

popleft()

從左邊刪除一個元素。並把這個元素返回。

假設是空隊列會拋出異常IndexError

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(1)

dq.append(2)

dq.append(3)

print(dq)

dq.popleft()

print(dq)

結果輸出例如以下:

deque([1, 2, 3], maxlen=5)

deque([2, 3], maxlen=5)

remove(value)

刪除指定值value的元素,假設沒有找到,會拋出異常IndexError

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(9)

dq.append(2)

dq.append(3)

dq.append(8)

print(dq)

dq.remove(9)

print(dq)

結果輸出例如以下:

deque([9, 2, 3, 8], maxlen=5)

deque([2, 3, 8], maxlen=5)

reverse()

把全部元素進行反向排序,返回None

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(9)

dq.append(2)

dq.append(3)

dq.append(8)

print(dq)

dq.reverse()

print(dq)

結果輸出例如以下:

deque([9, 2, 3, 8], maxlen=5)

deque([8, 3, 2, 9], maxlen=5)

rotate(n)

假設n是正值,則從右邊彈出元素,插入到左邊,n是代表操作幾次。

假設n是負值,剛好相反,表示從左邊彈出元素。插入到右邊。大體上等於d.appendlef(d.pop())

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(9)

dq.append(2)

dq.append(3)

dq.append(8)

print(dq)

dq.rotate(2)

print(dq)

dq.rotate(-2)

print(dq)

結果輸出例如以下:

deque([9, 2, 3, 8], maxlen=5)

deque([3, 8, 9, 2], maxlen=5)

deque([9, 2, 3, 8], maxlen=5)

deque類提供一個僅僅讀的屬性:

maxlen

返回列表同意的最大長度,假設沒有設置返回None

樣例:

#python 3.4

import collections

dq = collections.deque(maxlen = 5)

dq.append(9)

dq.append(2)

dq.append(3)

dq.append(8)

print(dq.maxlen)

結果輸出例如以下:

5

deque類支持叠代、選取、len(d)reversed(d)copy.copy(d)copy.deepcopy(d)等操作,同一時候也支持in操作符,以使用索引d[-1]的操作。



蔡軍生 QQ:9073204 深圳

5.3.3 deque對象