国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python cookbook(數據結構與算法)實現優先級隊列的方法示例

Python cookbook(數據結構與算法)實現優先級隊列的方法示例

2021-01-16 00:14壟上行 Python

這篇文章主要介紹了Python cookbook(數據結構與算法)實現優先級隊列的方法,結合實例形式分析了Python中基于給定優先級進行隊列元素排序的相關操作技巧,需要的朋友可以參考下

本文實例講述了Python實現優先級隊列的方法。分享給大家供大家參考,具體如下:

問題:要實現一個隊列,它能夠以給定的優先級對元素排序,且每次pop操作時都會返回優先級最高的那個元素;

解決方案:采用heapq模塊實現一個簡單的優先級隊列

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# example.py
#
# Example of a priority queue
import heapq
class PriorityQueue:
  def __init__(self):
    self._queue = []
    self._index = 0
  def push(self, item, priority):
    heapq.heappush(self._queue, (-priority, self._index, item))
    self._index += 1
  def pop(self):
    return heapq.heappop(self._queue)[-1]
# Example use
class Item:
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    return 'Item({!r})'.format(self.name)
q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1)
print("Should be bar:", q.pop())
print("Should be spam:", q.pop())
print("Should be foo:", q.pop())
print("Should be grok:", q.pop())
?
1
2
3
4
5
6
7
8
9
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Should be bar: Item('bar')
Should be spam: Item('spam')
Should be foo: Item('foo')
Should be grok: Item('grok')
>>>

可以看出:第一次執行pop()操作時返回的元素具有最高的優先級;對于相同優先級的兩個元素(foo和gork)返回的順序同它們插入到隊列時的順序相同。

在這段代碼中,隊列以元組(-priority, self._index, item)的形式組成,priority取負值是為了隊列按照從高到低的順序排列,這和堆默認的從小到大的排序相反。

變量index的作用是對相同優先級的元素以適當的順序排列,特別對同優先級的元素間做比較操作時扮演了重要的角色。

Item實例無法進行次序比較:

?
1
2
3
4
5
6
7
8
9
a=Item('foo')
b=Item('bar')
print('a<b: ',a<b)
>>>
Traceback (most recent call last):
 File "D:\4autotests\02script\python-cookbook\python-cookbook-master\src\1\5.implementing_a_priority_queue\example.py", line 27, in <module>
  print('a<b: ',a<b)
TypeError: unorderable types: Item() < Item()
>>>

如果以元組(priority,  item)的形式來表示元素,只要優先級不同,就可進行比較:

?
1
2
3
4
5
6
7
8
9
10
11
12
a=(1,Item('foo'))
b=(5,Item('bar'))
c=(1,Item('gork'))
print('a<b: ',a<b)
print('a<c: ',a<c)
>>>
a<b: True
Traceback (most recent call last):
 File "D:\4autotests\02script\python-cookbook\python-cookbook-master\src\1\5.implementing_a_priority_queue\example.py", line 29, in <module>
  print('a<c: ',a<c)
TypeError: unorderable types: Item() < Item()
>>>

引入額外的索引值,以(priority, index, item)的方式建立元組,就可以避免相同優先級無法比較的問題,因為沒有哪兩個元組會有相同的index值;

?
1
2
3
4
5
6
7
8
9
a=(1,0,Item('foo'))
b=(5,1,Item('bar'))
c=(1,2,Item('gork'))
print('a<b: ',a<b)
print('a<c: ',a<c)
>>>
a<b: True
a<c: True
>>>

如果想將這個隊列用于線程間通信,還需要增加適當的鎖和信號機制。

(代碼摘自《Python Cookbook》)

希望本文所述對大家Python程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/apple2016/p/5744620.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲免费影院 | 久久综合av | 在线黄 | 免费高清av | 伊人激情网 | 精品人成| 国产成人91| 日韩在线小视频 | 国产精品视频网站 | 日韩成人免费在线 | 欧美福利在线观看 | 91精品一区二区三区久久久久久 | 福利国产片 | 成人网av| 亚洲国产精品99久久久久久久久 | 亚洲视屏| 欧美成人二区 | 欧美一级片在线 | 免费观看黄色av网站 | 日韩国产在线 | 国产免费爽爽视频在线观看 | 美日韩精品视频 | 99视频在线免费观看 | 日韩av片无码一区二区不卡电影 | 成人片网址| 亚洲精品视频在线看 | 五月天一区二区 | 亚洲视频在线观看免费 | 日韩成人在线免费观看 | 四虎影视永久免费观看 | 免费在线一区二区 | 久久狠狠 | 亚洲www啪成人一区二区 | 亚洲性视频在线 | 欧美日韩电影 | 欧美在线免费观看 | 激情久久综合网 | 黄色一级片一级片 | 高清国产一区二区三区 | 精品天堂| 久久久国际精品 |