欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

python3 deque(双向队列)的详细介绍

程序员文章站 2022-03-14 18:02:02
...
创建双向队列
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(双向队列)的详细介绍的详细内容,更多请关注其它相关文章!

相关标签: python3 deque