Python中棧、隊列和優先級隊列的實現

關於我
編程界的一名小小程序猿,目前在一個創業團隊任team lead,技術棧涉及Android、Python、Java和Go,這個也是咱們團隊的主要技術棧。 聯繫:hylinux1024@gmail.comlinux

棧、隊列和優先級隊列都是很是基礎的數據結構。Python做爲一種「編碼高效」的語言,對這些基礎的數據結構都有比較好的實現。在業務需求開發過程當中,不該該重複造輪子,今天就來看看些數據結構都有哪些實現。編程

0x00 棧(Stack)

棧是一種LIFO(後進先出)的數據結構,有入棧(push)、出棧(pop)兩種操做,且只能操做棧頂元素。小程序

Python中有多種能夠實現棧的數據結構。數組

一、list

listPython內置的列表數據結構,它支持棧的特性,有入棧和出棧操做。只不過用list實現棧性能不是特別好。
由於list內部是經過一個動態擴容的數組來實現的。當增減元素時就有可能會觸發擴容操做。若是在list的頭部增減元素,也會移動整個列表。安全

如要使用list來實現一個棧的話,可使用listappend()(入棧)、pop()(出棧)方法。數據結構

>>> s = []
>>> s.append('one')
>>> s.append('two')
>>> s.append(3)
>>> s
['one', 'two', 3]
>>> s.pop()
3
>>> s.pop()
'two'
>>> s.pop()
'one'
>>> s.pop()
IndexError: pop from empty list
複製代碼

二、collections.deque

deque類是一種雙端隊列。在Python中它就是一個雙向列表,能夠以經常使用時間在兩端執行添加和刪除元素的操做,很是高效,因此它既能夠實現棧也能夠實現隊列。併發

若是要在Python實現一個棧,那麼應該優先選擇deque,而不是listapp

deque的入棧和出棧方法也分別是append()pop()性能

>>> from collections import deque
>>> s = deque()
>>> s.append('eat')
>>> s.append('sleep')
>>> s.append('code')
>>> s
deque(['eat', 'sleep', 'code'])
>>> s.pop()
'code'
>>> s.pop()
'sleep'
>>> s.pop()
'eat'
>>> s.pop()
IndexError: pop from an empty deque
複製代碼

三、queue.LifoQueue

顧名思義,這個就是一個棧。不過它是線程安全的,若是要在併發的環境下使用,那麼就能夠選擇使用LifoQueue。 它入棧和出棧操做是使用put()get(),其中get()LifoQueue爲空時會阻塞。學習

>>> from queue import LifoQueue
>>> s = LifoQueue()
>>> s.put('eat')
>>> s.put('sleep')
>>> s.put('code')
>>> s
<queue.LifoQueue object at 0x109dcfe48>
>>> s.get()
'code'
>>> s.get()
'sleep'
>>> s.get()
'eat'
>>> s.get()
# 阻塞並一直等待直到棧不爲空
複製代碼

0x01 隊列(Queue)

隊列是一種FIFO(先進先出)的數據結構。它有入隊(enqueue)、出隊(dequeue)兩種操做,並且也是常數時間的操做。

Python中可使用哪些數據結構來實現一個隊列呢?

一、list

list能夠實現一個隊列,但它的入隊、出隊操做就不是很是高效了。由於list是一個動態列表,在隊列的頭部執行出隊操做時,會發生整個元素的移動。

使用list來實現一個隊列時,用append()執行入隊操做,使用pop(0)方法在隊列頭部執行出隊操做。因爲在list的第一個元素進行操做,因此後續的元素都會向前移動一位。所以list來實現隊列是不推薦的。

>>> q = []
>>> q.append('1')
>>> q.append('2')
>>> q.append('three')

>>> q.pop(0)
'1'
>>> q.pop(0)
'2'
>>> q.pop(0)
'three'
>>> q.pop(0)
IndexError: pop from empty list
複製代碼

二、collections.deque

從上文咱們已經知道deque是一個雙向列表,它能夠在列表兩端以常數時間進行添加刪除操做。因此用deque來實現一個隊列是很是高效的。
deque入隊操做使用append()方法,出隊操做使用popleft()方法。

>>> from collections import deque
>>> q = deque()
>>> q.append('eat')
>>> q.append('sleep')
>>> q.append('code')
>>> q
deque(['eat', 'sleep', 'code'])
# 使用popleft出隊
>>> q.popleft()
'eat'
>>> q.popleft()
'sleep'
>>> q.popleft()
'code'
>>> q.popleft()
IndexError: pop from an empty deque
複製代碼

三、queue.Queue

一樣地,若是要在併發環境下使用隊列,那麼選擇線程安全的queue.Queue
LifoQueue相似,入隊和出隊操做分別是put()get()方法,get()在隊列爲空時會一直阻塞直到有元素入隊。

>>> from queue import Queue
>>> q = Queue()
>>> q.put('eat')
>>> q.put('sleep')
>>> q.put('code')
>>> q
<queue.Queue object at 0x110564780>
>>> q.get()
'eat'
>>> q.get()
'sleep'
>>> q.get()
'code'
# 隊列爲空不要執行等待
>>> q.get_nowait()
_queue.Empty
>>> q.put('111')
>>> q.get_nowait()
'111'
>>> q.get()
# 隊列爲空時,會一直阻塞直到隊列不爲空
複製代碼

四、multiprocessing.Queue

多進程版本的隊列。若是要在多進程環境下使用隊列,那麼應該選擇multiprocessing.Queue

一樣地,它的入隊出隊操做分別是put()get()get()方法在隊列爲空,會一直阻塞直到隊列不爲空。

>>> from multiprocessing import Queue
>>> q = Queue()
>>> q.put('eat')
>>> q.put('sleep')
>>> q.put('code')
>>> q
<multiprocessing.queues.Queue object at 0x110567ef0>
>>> q.get()
'eat'
>>> q.get()
'sleep'
>>> q.get()
'code'
>>> q.get_nowait()
_queue.Empty
>>> q.get()
# 隊列爲空時,會一直阻塞直到隊列不爲空
複製代碼

0x02 優先級隊列(PriorityQueue)

一個近乎排序的序列裏可使用優先級隊列這種數據結構,它能高效獲取最大或最小的元素。

在調度問題的場景中常常會用到優先級隊列。它主要有獲取最大值或最小值的操做和入隊操做。

一、list

使用list能夠實現一個優先級隊列,但它並不高效。由於當要獲取最值時須要排序,而後再獲取最值。一旦有新的元素加入,再次獲取最值時,又要從新排序。因此並推薦使用。

二、heapq

通常來講,優先級隊列都是使用堆這種數據結構來實現。而heapq就是Python標準庫中堆的實現。heapq默認狀況下實現的是最小堆。

入隊操做使用heappush(),出隊操做使用heappop()

>>> import heapq
>>> q = []
>>> heapq.heappush(q, (2, 'code'))
>>> heapq.heappush(q, (1, 'eat'))
>>> heapq.heappush(q, (3, 'sleep'))
>>> q
[(1, 'eat'), (2, 'code'), (3, 'sleep')]
>>> while q:
	next_item = heapq.heappop(q)
	print(next_item)

	
(1, 'eat')
(2, 'code')
(3, 'sleep')
複製代碼

三、queue.PriorityQueue

queue.PriorityQueue內部封裝了heapq,不一樣的是它是線程安全的。在併發環境下應該選擇使用PriorityQueue

>>> from queue import PriorityQueue
>>> q = PriorityQueue()
>>> q.put((2, 'code'))
>>> q.put((1, 'eat'))
>>> q.put((3, 'sleep'))
>>> while not q.empty():
	next_item = q.get()
	print(next_item)

(1, 'eat')
(2, 'code')
(3, 'sleep')
複製代碼

0x03 總結一下

不少基礎的數據結構在Python中已經實現了的,咱們不該該重複造輪子,應該選擇這些數據結構來實現業務需求。

collections.deque是一種雙向鏈表,在單線程的狀況下,它能夠用來實現StackQueue。而heapq模塊能夠幫咱們實現高效的優先級隊列。

若是要在多併發的狀況下使用StackQueuePriorityQueue的話,那麼應該選用queue模塊下類:

  • 實現Stackqueue.LifoQueue
  • 實現Queuequeue.Queuemultiprocessing.Queue
  • 實現PriorityQueuequeue.PriorityQueue
  • 以上這些類都有put()get()方法,且get()會在棧/隊列爲空時阻塞。

0x04 學習資料

  • Python Tricks: A Buffet of Awesome Python Features ——Dan Bader
相關文章
相關標籤/搜索