长度限制的队列Python
生活随笔
收集整理的這篇文章主要介紹了
长度限制的队列Python
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很久以前就在cookbook上看到過這個,但是自己一直都沒有研究
這次算是完成了之前立下的一個小flag了吧
這個隊列非常簡單,就是存在于collections中, 只需要自己創建的時候設置一下maxlen就好了~
具體怎么使用的話,跟一般的隊列沒有什么太多的區別
這個隊列本質上是用雙向列表實現的,所以在頭部插入或者彈出的時候,都只需要改變一下,也就是O(1)的復雜度,在list中,則是O(n)的復雜度了
>>> from collections import deque >>> time = 0 >>> def search(): ... pre_deq = deque(maxlen=5) ... global time ... count = 100 ... while time < count: ... yield pre_deq, time ... time += 1 ... pre_deq.append(time) ... >>> for dq, t in search(): ... print(t) ... for d in dq: ... print(d, end=',') ... print() ... 01 1, 2 1,2, 3 1,2,3, 4 1,2,3,4, 5 1,2,3,4,5, 6 2,3,4,5,6, 7 3,4,5,6,7, 8 4,5,6,7,8, 9 5,6,7,8,9, 10 6,7,8,9,10, 11 7,8,9,10,11, 12 8,9,10,11,12, 13 9,10,11,12,13, 14 10,11,12,13,14, 15 11,12,13,14,15, 16 12,13,14,15,16, 17 13,14,15,16,17, 18 14,15,16,17,18, 19 15,16,17,18,19, 20 16,17,18,19,20, 21 17,18,19,20,21, 22 18,19,20,21,22, 23 19,20,21,22,23, 24 20,21,22,23,24, 25 21,22,23,24,25, 26 22,23,24,25,26, 27 23,24,25,26,27, 28 24,25,26,27,28, 29 25,26,27,28,29, 30 26,27,28,29,30, 31 27,28,29,30,31, 32 28,29,30,31,32, 33 29,30,31,32,33, 34 30,31,32,33,34, 35 31,32,33,34,35, 36 32,33,34,35,36, 37 33,34,35,36,37, 38 34,35,36,37,38, 39 35,36,37,38,39, 40 36,37,38,39,40, 41 37,38,39,40,41, 42 38,39,40,41,42, 43 39,40,41,42,43, 44 40,41,42,43,44, 45 41,42,43,44,45, 46 42,43,44,45,46, 47 43,44,45,46,47, 48 44,45,46,47,48, 49 45,46,47,48,49, 50 46,47,48,49,50, 51 47,48,49,50,51, 52 48,49,50,51,52, 53 49,50,51,52,53, 54 50,51,52,53,54, 55 51,52,53,54,55, 56 52,53,54,55,56, 57 53,54,55,56,57, 58 54,55,56,57,58, 59 55,56,57,58,59, 60 56,57,58,59,60, 61 57,58,59,60,61, 62 58,59,60,61,62, 63 59,60,61,62,63, 64 60,61,62,63,64, 65 61,62,63,64,65, 66 62,63,64,65,66, 67 63,64,65,66,67, 68 64,65,66,67,68, 69 65,66,67,68,69, 70 66,67,68,69,70, 71 67,68,69,70,71, 72 68,69,70,71,72, 73 69,70,71,72,73, 74 70,71,72,73,74, 75 71,72,73,74,75, 76 72,73,74,75,76, 77 73,74,75,76,77, 78 74,75,76,77,78, 79 75,76,77,78,79, 80 76,77,78,79,80, 81 77,78,79,80,81, 82 78,79,80,81,82, 83 79,80,81,82,83, 84 80,81,82,83,84, 85 81,82,83,84,85, 86 82,83,84,85,86, 87 83,84,85,86,87, 88 84,85,86,87,88, 89 85,86,87,88,89, 90 86,87,88,89,90, 91 87,88,89,90,91, 92 88,89,90,91,92, 93 89,90,91,92,93, 94 90,91,92,93,94, 95 91,92,93,94,95, 96 92,93,94,95,96, 97 93,94,95,96,97, 98 94,95,96,97,98, 99 95,96,97,98,99, >>>總結
以上是生活随笔為你收集整理的长度限制的队列Python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【解决方法】Panda read_csv
- 下一篇: 【2018年更新】Sublime tex