<legend id='we6RQ'><style id='we6RQ'><dir id='we6RQ'><q id='we6RQ'></q></dir></style></legend>

    <i id='we6RQ'><tr id='we6RQ'><dt id='we6RQ'><q id='we6RQ'><span id='we6RQ'><b id='we6RQ'><form id='we6RQ'><ins id='we6RQ'></ins><ul id='we6RQ'></ul><sub id='we6RQ'></sub></form><legend id='we6RQ'></legend><bdo id='we6RQ'><pre id='we6RQ'><center id='we6RQ'></center></pre></bdo></b><th id='we6RQ'></th></span></q></dt></tr></i><div id='we6RQ'><tfoot id='we6RQ'></tfoot><dl id='we6RQ'><fieldset id='we6RQ'></fieldset></dl></div>
    • <bdo id='we6RQ'></bdo><ul id='we6RQ'></ul>
  1. <small id='we6RQ'></small><noframes id='we6RQ'>

      <tfoot id='we6RQ'></tfoot>

      如何在 Python 中实现优先级队列?

      How to implement Priority Queues in Python?(如何在 Python 中实现优先级队列?)

      <small id='PZfD3'></small><noframes id='PZfD3'>

        <tbody id='PZfD3'></tbody>

          <legend id='PZfD3'><style id='PZfD3'><dir id='PZfD3'><q id='PZfD3'></q></dir></style></legend>

          <i id='PZfD3'><tr id='PZfD3'><dt id='PZfD3'><q id='PZfD3'><span id='PZfD3'><b id='PZfD3'><form id='PZfD3'><ins id='PZfD3'></ins><ul id='PZfD3'></ul><sub id='PZfD3'></sub></form><legend id='PZfD3'></legend><bdo id='PZfD3'><pre id='PZfD3'><center id='PZfD3'></center></pre></bdo></b><th id='PZfD3'></th></span></q></dt></tr></i><div id='PZfD3'><tfoot id='PZfD3'></tfoot><dl id='PZfD3'><fieldset id='PZfD3'></fieldset></dl></div>
              <bdo id='PZfD3'></bdo><ul id='PZfD3'></ul>
              <tfoot id='PZfD3'></tfoot>
                本文介绍了如何在 Python 中实现优先级队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                很抱歉提出这么愚蠢的问题,但 Python 文档令人困惑......

                Sorry for such a silly question but Python docs are confusing...

                链接 1:队列实现http://docs.python.org/library/queue.html

                它说 Queue 有一个优先级队列的类.但我找不到如何实现它.

                It says that Queue has a class for the priority queue. But I could not find how to implement it.

                class Queue.PriorityQueue(maxsize=0)
                

                链接 2:堆实现http://docs.python.org/library/heapq.html

                这里他们说我们可以使用 heapq 间接实现优先级队列

                Here they say that we can implement priority queues indirectly using heapq

                pq = []                         # list of entries arranged in a heap
                entry_finder = {}               # mapping of tasks to entries
                REMOVED = '<removed-task>'      # placeholder for a removed task
                counter = itertools.count()     # unique sequence count
                
                def add_task(task, priority=0):
                    'Add a new task or update the priority of an existing task'
                    if task in entry_finder:
                        remove_task(task)
                    count = next(counter)
                    entry = [priority, count, task]
                    entry_finder[task] = entry
                    heappush(pq, entry)
                
                def remove_task(task):
                    'Mark an existing task as REMOVED.  Raise KeyError if not found.'
                    entry = entry_finder.pop(task)
                    entry[-1] = REMOVED
                
                def pop_task():
                    'Remove and return the lowest priority task. Raise KeyError if empty.'
                    while pq:
                        priority, count, task = heappop(pq)
                        if task is not REMOVED:
                            del entry_finder[task]
                            return task
                    raise KeyError('pop from an empty priority queue'
                

                Python 中最有效的优先级队列实现是什么?以及如何实现?

                Which is the most efficient priority queue implementation in Python? And how to implement it?

                推荐答案

                Queue模块中的版本是使用heapq模块实现,因此它们对于底层堆操作具有相同的效率.

                The version in the Queue module is implemented using the heapq module, so they have equal efficiency for the underlying heap operations.

                也就是说,Queue 版本速度较慢,因为它添加了锁、封装和一个不错的面向对象的 API.

                That said, the Queue version is slower because it adds locks, encapsulation, and a nice object oriented API.

                heapq 文档中显示的优先队列建议旨在显示如何向优先级队列添加其他功能(例如排序稳定性和更改先前入队任务的优先级的能力).如果您不需要这些功能,那么基本的 heappushheappop 功能将为您提供最快的性能.

                The priority queue suggestions shown in the heapq docs are meant to show how to add additional capabilities to a priority queue (such as sort stability and the ability to change the priority of a previously enqueued task). If you don't need those capabilities, then the basic heappush and heappop functions will give you the fastest performance.

                这篇关于如何在 Python 中实现优先级队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
              1. <small id='Ai4Vq'></small><noframes id='Ai4Vq'>

                        <bdo id='Ai4Vq'></bdo><ul id='Ai4Vq'></ul>
                        <tfoot id='Ai4Vq'></tfoot>
                        <legend id='Ai4Vq'><style id='Ai4Vq'><dir id='Ai4Vq'><q id='Ai4Vq'></q></dir></style></legend>
                          <tbody id='Ai4Vq'></tbody>
                          <i id='Ai4Vq'><tr id='Ai4Vq'><dt id='Ai4Vq'><q id='Ai4Vq'><span id='Ai4Vq'><b id='Ai4Vq'><form id='Ai4Vq'><ins id='Ai4Vq'></ins><ul id='Ai4Vq'></ul><sub id='Ai4Vq'></sub></form><legend id='Ai4Vq'></legend><bdo id='Ai4Vq'><pre id='Ai4Vq'><center id='Ai4Vq'></center></pre></bdo></b><th id='Ai4Vq'></th></span></q></dt></tr></i><div id='Ai4Vq'><tfoot id='Ai4Vq'></tfoot><dl id='Ai4Vq'><fieldset id='Ai4Vq'></fieldset></dl></div>