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

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

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

    • <bdo id='mdf5p'></bdo><ul id='mdf5p'></ul>
    1. Python 中内置的最大堆 API

      built-in max heap API in Python(Python 中内置的最大堆 API)
        <bdo id='Dfpza'></bdo><ul id='Dfpza'></ul>
      • <legend id='Dfpza'><style id='Dfpza'><dir id='Dfpza'><q id='Dfpza'></q></dir></style></legend>
          1. <small id='Dfpza'></small><noframes id='Dfpza'>

              <tbody id='Dfpza'></tbody>

            • <tfoot id='Dfpza'></tfoot>

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

                问题描述

                默认 heapq 是最小队列实现,想知道是否有最大队列选项?谢谢.

                Default heapq is min queue implementation and wondering if there is an option for max queue? Thanks.

                我尝试了使用 _heapify_max 作为最大堆的解决方案,但是如何动态处理 push/pop 元素?看来 _heapify_max 只能在初始化期间使用.

                I tried the solution using _heapify_max for max heap, but how to handle dynamically push/pop element? It seems _heapify_max could only be used during initialization time.

                import heapq
                
                def heapsort(iterable):
                    h = []
                    for value in iterable:
                        heapq.heappush(h, value)
                    return [heapq.heappop(h) for i in range(len(h))]
                
                if __name__ == "__main__":
                
                    print heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
                

                编辑,尝试 _heapify_max 似乎不适用于动态推送/弹出元素.我试过两种方法输出一样,输出都是,[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

                Edit, tried _heapify_max seems not working for dynamically push/pop elements. I tried both methods output the same, both output is, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

                def heapsort(iterable):
                    h = []
                    for value in iterable:
                        heapq.heappush(h, value)
                    return [heapq.heappop(h) for i in range(len(h))]
                
                def heapsort2(iterable):
                    h = []
                    heapq._heapify_max(h)
                    for value in iterable:
                        heapq.heappush(h, value)
                    return [heapq.heappop(h) for i in range(len(h))]
                
                if __name__ == "__main__":
                
                    print heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
                    print heapsort2([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
                

                提前致谢,林

                推荐答案

                过去我只是简单地使用 sortedcontainers 的 SortedList 为此,如:

                In the past I have simply used sortedcontainers's SortedList for this, as:

                > a = SortedList()
                > a.add(3)
                > a.add(2)
                > a.add(1)
                > a.pop()
                3
                

                它不是堆,但速度很快,可以根据需要直接工作.

                It's not a heap, but it's fast and works directly as required.

                如果你绝对需要它成为一个堆,你可以创建一个通用的否定类来保存你的项目.

                If you absolutely need it to be a heap, you could make a general negation class to hold your items.

                class Neg():
                    def __init__(self, x):
                        self.x = x
                
                    def __cmp__(self, other):
                        return -cmp(self.x, other.x)
                
                def maxheappush(heap, item):
                    heapq.heappush(heap, Neg(item))
                
                def maxheappop(heap):
                    return heapq.heappop(heap).x
                

                但这会占用更多内存.

                这篇关于Python 中内置的最大堆 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 数据框制作线图?)

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

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

                          <bdo id='LX28W'></bdo><ul id='LX28W'></ul>
                            <tbody id='LX28W'></tbody>
                        • <legend id='LX28W'><style id='LX28W'><dir id='LX28W'><q id='LX28W'></q></dir></style></legend>
                        • <tfoot id='LX28W'></tfoot>