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

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

      <tfoot id='UiWcK'></tfoot>

    1. <legend id='UiWcK'><style id='UiWcK'><dir id='UiWcK'><q id='UiWcK'></q></dir></style></legend>
      • <bdo id='UiWcK'></bdo><ul id='UiWcK'></ul>
    2. 具有自定义比较谓词的 heapq

      heapq with custom compare predicate(具有自定义比较谓词的 heapq)

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

      1. <legend id='a7TnM'><style id='a7TnM'><dir id='a7TnM'><q id='a7TnM'></q></dir></style></legend>

      2. <tfoot id='a7TnM'></tfoot>
        • <small id='a7TnM'></small><noframes id='a7TnM'>

              <tbody id='a7TnM'></tbody>
              • <bdo id='a7TnM'></bdo><ul id='a7TnM'></ul>
                本文介绍了具有自定义比较谓词的 heapq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用自定义排序谓词构建堆.由于进入它的值是用户定义"类型,我无法修改它们的内置比较谓词.

                I am trying to build a heap with a custom sort predicate. Since the values going into it are of 'user-defined' type, I cannot modify their built-in comparison predicate.

                有没有办法做类似的事情:

                Is there a way to do something like:

                h = heapq.heapify([...], key=my_lt_pred)
                h = heapq.heappush(h, key=my_lt_pred)
                

                或者更好的是,我可以将 heapq 函数包装在自己的容器中,这样我就不需要继续传递谓词了.

                Or even better, I could wrap the heapq functions in my own container so I don't need to keep passing the predicate.

                推荐答案

                根据heapq 文档,自定义堆顺序的方法是让堆上的每个元素成为一个元组,第一个元组元素是一个接受普通 Python 比较的元素.

                According to the heapq documentation, the way to customize the heap order is to have each element on the heap to be a tuple, with the first tuple element being one that accepts normal Python comparisons.

                heapq 模块中的函数有点麻烦(因为它们不是面向对象的),并且总是需要我们的堆对象(一个堆化列表)作为第一个参数显式传递.我们可以通过创建一个非常简单的包装类来用一块石头杀死两只鸟,它允许我们指定一个 key 函数,并将堆呈现为一个对象.

                The functions in the heapq module are a bit cumbersome (since they are not object-oriented), and always require our heap object (a heapified list) to be explicitly passed as the first parameter. We can kill two birds with one stone by creating a very simple wrapper class that will allow us to specify a key function, and present the heap as an object.

                下面的类保留一个内部列表,其中每个元素都是一个元组,其中第一个成员是一个键,在元素插入时使用 key 参数计算,在堆实例化时传递:

                The class below keeps an internal list, where each element is a tuple, the first member of which is a key, calculated at element insertion time using the key parameter, passed at Heap instantiation:

                # -*- coding: utf-8 -*-
                import heapq
                
                class MyHeap(object):
                   def __init__(self, initial=None, key=lambda x:x):
                       self.key = key
                       self.index = 0
                       if initial:
                           self._data = [(key(item), i, item) for i, item in enumerate(initial)]
                           self.index = len(self._data)
                           heapq.heapify(self._data)
                       else:
                           self._data = []
                
                   def push(self, item):
                       heapq.heappush(self._data, (self.key(item), self.index, item))
                       self.index += 1
                
                   def pop(self):
                       return heapq.heappop(self._data)[2]
                

                (额外的 self.index 部分是为了避免在评估的键值是平局并且存储的值不能直接比较时发生冲突 - 否则 heapq 可能会因 TypeError 而失败)

                (The extra self.index part is to avoid clashes when the evaluated key value is a draw and the stored value is not directly comparable - otherwise heapq could fail with TypeError)

                这篇关于具有自定义比较谓词的 heapq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                <tfoot id='2d7o4'></tfoot>
                <i id='2d7o4'><tr id='2d7o4'><dt id='2d7o4'><q id='2d7o4'><span id='2d7o4'><b id='2d7o4'><form id='2d7o4'><ins id='2d7o4'></ins><ul id='2d7o4'></ul><sub id='2d7o4'></sub></form><legend id='2d7o4'></legend><bdo id='2d7o4'><pre id='2d7o4'><center id='2d7o4'></center></pre></bdo></b><th id='2d7o4'></th></span></q></dt></tr></i><div id='2d7o4'><tfoot id='2d7o4'></tfoot><dl id='2d7o4'><fieldset id='2d7o4'></fieldset></dl></div>
              • <small id='2d7o4'></small><noframes id='2d7o4'>

              • <legend id='2d7o4'><style id='2d7o4'><dir id='2d7o4'><q id='2d7o4'></q></dir></style></legend>
                  <tbody id='2d7o4'></tbody>

                        <bdo id='2d7o4'></bdo><ul id='2d7o4'></ul>