<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 問題 怎樣實現一個按優先級排序的隊列? 并且在這個隊列上面每次pop操作總是返回優先級最高的那個元素 ## 解決方案 下面的類利用heapq模塊實現了一個簡單的優先級隊列: 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] 下面是它的使用方式: >>> 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) >>> q.pop() Item('bar') >>> q.pop() Item('spam') >>> q.pop() Item('foo') >>> q.pop() Item('grok') >>> 仔細觀察可以發現,第一個pop()操作返回優先級最高的元素。另外注意到如果兩個有著相同優先級的元素(foo 和 grok),pop操作按照它們被插入到隊列的順序返回的。 ## 討論 這一小節我們主要關注heapq模塊的使用。函數 `heapq.heappush()` 和 `heapq.heappop()` 分別在隊列_queue上插入和刪除第一個元素,并且隊列_queue保證第一個元素擁有最小優先級(1.4節已經討論過這個問題)。heappop()函數總是返回”最小的”的元素,這就是保證隊列pop操作返回正確元素的關鍵。另外,由于push和pop操作時間復雜度為O(N),其中N是堆的大小,因此就算是N很大的時候它們運行速度也依舊很快。 在上面代碼中,隊列包含了一個 `(-priority, index, item)` 的元組。優先級為負數的目的是使得元素按照優先級從高到低排序。這個跟普通的按優先級從低到高排序的堆排序恰巧相反。 index變量的作用是保證同等優先級元素的正確排序。通過保存一個不斷增加的index下標變量,可以確保元素安裝它們插入的順序排序。而且,index變量也在相同優先級元素比較的時候起到重要作用。 為了闡明這些,先假定Item實例是不支持排序的: >>> a = Item('foo') >>> b = Item('bar') >>> a < b Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: Item() < Item() >>> 如果你使用元組 `(priority, item)` ,只要兩個元素的優先級不同就能比較。 但是如果兩個元素優先級一樣的話,那么比較操作就會跟之前一樣出錯: ~~~ >>> a = (1, Item('foo')) >>> b = (5, Item('bar')) >>> a < b True >>> c = (1, Item('grok')) >>> a < c Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: Item() < Item() >>> ~~~ 通過引入另外的index變量組成三元組(priority, index, item),就能很好的避免上面的錯誤,因為不可能有兩個元素有相同的index值。Python在做元組比較時候,如果前面的比較以及可以確定結果了,后面的比較操作就不會發生了: >>> a = (1, 0, Item('foo')) >>> b = (5, 1, Item('bar')) >>> c = (1, 2, Item('grok')) >>> a < b True >>> a < c True >>> 如果你想在多個線程中使用同一個隊列,那么你需要增加適當的鎖和信號量機制。可以查看12.3小節的例子演示是怎樣做的。 heapq模塊的官方文檔有更詳細的例子程序以及對于堆理論及其實現的詳細說明。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看