<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

      时间:2023-09-29
        <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:学习python中的Queue模块(如何运行) 下一篇:Python多处理使用队列写入同一个文件

                相关文章

                <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>
                  1. <legend id='LX28W'><style id='LX28W'><dir id='LX28W'><q id='LX28W'></q></dir></style></legend>
                  2. <tfoot id='LX28W'></tfoot>