Python-堆

  1. 使用
  2. 自定义排序

使用

from heapq import * 
# 建立堆
heap=[]

# 将x压入堆中
heappush(heap, x)    
# 从堆中弹出最小的元素              
heappop(heap)   

# 让列表具备堆特征,函数heapify通过执行尽可能少的移位操作将列表变成合法的堆(即具备堆特征)。如果你的堆并不是使用heappush创建的,应在使用heappush和heappop之前使用这个函数。
heapify(heap) 
# 弹出最小的元素,并将x压入堆中
heapreplace(heap, x)        

# 返回iter中n个最大的元素
nlargest(n, iter)                           
# 返回iter中n个最小的元素            
nsmallest(n, iter)                                   

自定义排序

class Document:
    def __init__(self, _id, con, goal):
        self.id = str(_id)
        self.content = con
        self.goal = goal

    # 按照goal,从小到大排序
    def __lt__(self, other):
        if self.goal < other.goal:
            return True
        else:
            return False

d1=Document('1',"fda",23)
d2=Document('2',"fda",2)
d3=Document('3',"fda",245)

li=[d1,d2,d3]
li.sort()

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 changzeyan@foxmail.com

×

喜欢就点赞,疼爱就打赏