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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python教程:collections的deque()方法

發布時間:2025/3/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python教程:collections的deque()方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建雙向隊列

import collections d = collections.deque()

append(往右邊添加一個元素)

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

appendleft(往左邊添加一個元素)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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(返回指定元素的出現次數)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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(從隊列左邊擴展一個列表的元素)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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(在指定位置插入元素)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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(獲取最左邊一個元素,并在隊列中刪除)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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(隊列反轉)

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:778463939 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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']) 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Python教程:collections的deque()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。