ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python双向队列deque实践与总结

2021-09-28 19:01:53  阅读:190  来源: 互联网

标签:deque apple item python deq 队列 print demonstration


背景

1.什么是双端队列

deque的英文意思是Double-Ended Queue,deque是为了在两端高效实现插入和删除操作的双向列表,适合用于队列和栈:deque除了实现list的append()和pop()外,还支持appendleft()和popleft(),这样就可以非常高效地往头部或者尾部添加或删除元素

基本概念

与常见的list使用区别如下所示


常用的接口

deque:append和popleft

Deque基本表现

  • 优点

(1)deque接收GIL管理,线程安全。list没有GIL锁,所以线程不安全。也就是说,在并发场景中,list可能会导致一致性问题,而deque不会。

(2)deque支持固定长度。当长度满了,当我们继续使用append时,它会自动弹出最早插入的数据。
(3) deque 更快

deque demo

See the following code example of Deque in Python.


#importing deque from collections module
from collections import deque

#initializing the deque object
deq = deque(['apple', 'mango', 'orange'])
#printing the initial deque object 
print("Printing the initial deque object items \n" , deq, '\n')
#append(x) demonstration
print("Appending a new element to the right of deque")
deq.append('papaya')
print(deq , '\n') 
#appendleft(x) demonstration
print("Appending a new element to the left of deque")
deq.appendleft('strawberry')
print(deq , '\n') 
#count(x) demonstration
print("Counting the the occurrence of apple in deque")
print(deq.count('apple') , '\n') 
#extend(iterable) demonstration
deq.extend(['grapes', 'watermelon'])
print("Extending the deque with new items on the right")
print(deq, "\n") 
extendleft(iterable) demonstration
deq.extendleft(['pear', 'guava'])
print("Extending the deque with new items on the left")
print(deq, "\n") 
#index(x [, start [, stop]]) demonstration
print("Finding the index of an item")
print(deq.index('strawberry'), "\n") 
#insert(i,x) demonstration
print("Inserting an item to the deque by specifiying the index")
deq.insert(2,'banana')
print(deq, "\n") 
#pop() demonstration
print("Popping out last item from the right")
print(deq.pop(), "\n") 
#popleft() demonstration
print("Popping out first item from the left")
print(deq.popleft(), "\n") 
#remove() demonstration
print("Removing an item from the deque")
deq.remove("apple")
print(deq, "\n") 

运行结果:


Printing the initial deque object items
deque(['apple', 'mango', 'orange'])

Appending a new element to the right of deque
deque(['apple', 'mango', 'orange', 'papaya'])

Appending a new element to the left of deque
deque(['strawberry', 'apple', 'mango', 'orange', 'papaya'])

Counting the the occurrence of apple in deque
1

Extending the deque with new items on the right
deque(['strawberry', 'apple', 'mango', 'orange', 'papaya', 'grapes', 'watermelon'])

Extending the deque with new items on the left
deque(['guava', 'pear', 'strawberry', 'apple', 'mango', 'orange', 'papaya', 'grapes', 'watermelon'])

Finding the index of an item
2

Inserting an item to the deque by specifiying the index
deque(['guava', 'pear', 'banana', 'strawberry', 'apple', 'mango', 'orange', 'papaya', 'grapes', 'watermelon'])

Popping out last item from the right
watermelon

Popping out first item from the left
guava

Removing an item from the deque
deque(['pear', 'banana', 'strawberry', 'mango', 'orange', 'papaya', 'grapes'])

总结

如果需要使用一个容器来处理数据 请使用deque

标签:deque,apple,item,python,deq,队列,print,demonstration
来源: https://www.cnblogs.com/wnw-nicholas/p/15349412.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有