从heapq pythond出最大值,Python中有最大堆吗?[重复]

从heapq pythond出最大值,Python中有最大堆吗?[重复],第1张

从heapq pythond出最大值,Python中有最大堆吗?[重复]

将对象包装在反向比较包装器中:

import [email protected]_orderingclass ReverseCompare(object):    def __init__(self, obj):        self.obj = obj    def __eq__(self, other):        return isinstance(other, ReverseCompare) and self.obj == other.obj    def __le__(self, other):        return isinstance(other, ReverseCompare) and self.obj >= other.obj    def __str__(self):        return str(self.obj)    def __repr__(self):        return '%s(%r)' % (self.__class__.__name__, self.obj)

用法

import heapqletters = 'axuebizjmf'heap = map(ReverseCompare, letters)heapq.heapify(heap)print heapq.heappop(heap) # prints z


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4957934.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存