日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python3 deque(双向队列)

發(fā)布時間:2025/7/14 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 deque(双向队列) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載:https://www.cnblogs.com/zhenwei66/p/6598996.html

?

創(chuàng)建雙向隊(duì)列

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(清空隊(duì)列)

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(返回指定元素的出現(xiàn)次數(shù))

import collections d = collections.deque() d.append(1) d.append(1) print(d.count(1))#輸出:2

extend(從隊(duì)列右邊擴(kuò)展一個列表的元素)

import collections d = collections.deque() d.append(1) d.extend([3,4,5]) print(d)#輸出:deque([1, 3, 4, 5])

extendleft(從隊(duì)列左邊擴(kuò)展一個列表的元素)

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)) #指定查找區(qū)間#輸出: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(獲取最右邊一個元素,并在隊(duì)列中刪除)

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(獲取最左邊一個元素,并在隊(duì)列中刪除)

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(隊(duì)列反轉(zhuǎn))

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) #指定次數(shù),默認(rèn)1次 print(d)#輸出:deque(['d', 'e', 'a', 'b', 'c'])

轉(zhuǎn)載于:https://www.cnblogs.com/saolv/p/9839711.html

總結(jié)

以上是生活随笔為你收集整理的python3 deque(双向队列)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。