python collection 中的队列

认识中的队列

  在以前的认知里,队列是先进先出,就是一头进,一头出,Queue。而无意间看到了deque 双向队列。

即从该队列的头或者尾部都能插入和移除元素。而起时间复杂度竟然是一样的!O(1),是不是想起了和列表

有点像,但列表从尾部和头部删除或插入的时间复杂度可差别很大的。但还是有点相像。

双向列表的属性和方法

rom collections import deque
#实例化一个deque队列
d=deque()
#list
list_obj = list()
print(d)
#从右边插入数据
d.append("yang")
list_obj.append("yang")
#add one record from left
d.appendleft("zhang")
print(d)
#
for item in d:
    print(item)
#the len
print("the len of duque is {}".format(len(d)))
#pop one from right
d.pop()
print(d)
d.append("yang")
#pop item from left
d.popleft()
d.appendleft("zhang")
#双向队列中元素"zhang"的个数
print(d.count("zhang"))
#队列d右边扩展多个元素
d.extend("test")
#
d.extendleft("left")
#默认向右旋转,步长为1, 如果步长为-1则向左旋转
d.rotate()
print(d)
原文地址:https://www.cnblogs.com/taoyoung/p/9826230.html