建立雙向佇列
import collections d = collections.deque()
append(往右邊新增一個元素)
import collections d = collections.deque() d.append(1) d.append(2)print(d)#输出:deque([1, 2])
appendleft(左邊加一個元素)
import collections d = collections.deque() d.append(1) d.appendleft(2)print(d)#输出:deque([2, 1])
clear(清空佇列)
import collections d = collections.deque() d.append(1) d.clear()print(d)#输出:deque([])
copy(淺拷貝)
import collections d = collections.deque() d.append(1) new_d = d.copy()print(new_d)#输出:deque([1])
count(傳回指定元素的出現次數)
import collections d = collections.deque() d.append(1) d.append(1)print(d.count(1))#输出:2
extend(從佇列右邊擴充一個清單的元素)
import collections d = collections.deque() d.append(1) d.extend([3,4,5])print(d)#输出:deque([1, 3, 4, 5])
extendleft(從佇列左邊擴展一個清單的元素)
import collections d = collections.deque() d.append(1) d.extendleft([3,4,5])print(d)# # #输出:deque([5, 4, 3, 1])
index(尋找某個元素的索引位置)
import collections d = collections.deque() d.extend(['a','b','c','d','e'])print(d)print(d.index('e'))print(d.index('c',0,3)) #指定查找区间#输出:deque(['a', 'b', 'c', 'd', 'e'])# 4# 2
insert(在指定位置插入元素)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) d.insert(2,'z')print(d)#输出:deque(['a', 'b', 'z', 'c', 'd', 'e'])
pop(取得最右邊一個元素,並在佇列中刪除)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) x = d.pop()print(x,d)#输出:e deque(['a', 'b', 'c', 'd'])
popleft(取得最左邊一個元素,並在佇列中刪除)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) x = d.popleft()print(x,d)#输出:a deque(['b', 'c', 'd', 'e'])
remove(刪除指定元素)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) d.remove('c')print(d)#输出:deque(['a', 'b', 'd', 'e'])
reverse(佇列反轉)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) d.reverse()print(d)#输出:deque(['e', 'd', 'c', 'b', 'a'])
rotate(把右邊元素放到左邊)
import collections d = collections.deque() d.extend(['a','b','c','d','e']) d.rotate(2) #指定次数,默认1次print(d)#输出:deque(['d', 'e', 'a', 'b', 'c'])
以上是python3 deque(雙向隊列)的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!