Python:优先队列的使用及类的自定义比较函数

Priority_queue模块

该模块定义的优先级队列,其内部使用了 heapq 模块,所以它的时间复杂度和heapq是相同的。

当一个对象的所有元素都是可比较的时,默认情况下是根据队列中的对象的第一个元素进行排序,越小的优先级越高,排在越前面。当第一个元素相同时,依次比较后续的元素的大小来进行排序。

由于 PriorityQueue 是继承自 Queue 类,所以很多函数的用法可以直接参照于 Queue 类中的函数。

from queue import PriorityQueue as PQ
pq = PQ()
pq.put((1, 'a'))
pq.put((2, 'c'))
pq.put((2, 'b'))
pq.put((2, 'b'))
print(pq.queue) # [(1, 'a'), (2, 'b'), (2, 'b'), (2, 'c')]
item0 = pq.get() # (1, 'a')
print(pq.queue) # [(2, 'b'), (2, 'b'), (2, 'c')]

print(pq.qsize()) # 优先队列的尺寸

while not pq.empty():
    print(pq.get())

对于自定义的类使用优先队列,在入队的时候需要根据类的某一属性进行比较。

方法:使用重载方法__lt__,__lt__是python中用于进行特定比较的方法。

 

import queue
class person(object):
    def __init__(self,name,score):
        self.name = name
        self.score = score
    def __lt__(self, other):
        return self.score > other.score
 
 
p1 = person("张三",15)
p2 = person("李四",23)
p3 = person("王五",12)
p4 = person("朱五",32)
que = queue.PriorityQueue()
que.put(p1)
que.put(p2)
que.put(p4)
que.put(p3)
 
print(que.get().name)
print(que.get().name)
print(que.get().name)
print(que.get().name)

其中,__lt__定义了根据score属性进行从大到小的排列。即当PriorityQueue入队一个类实例的时候,会自动根据score属性进行比较。

 这里给出一个例子

 我们需要按照小白鼠的重量从大到小排序,再输出它们的帽子颜色。 


class Node : 
    def __init__(self, w, col) : 
        self.w = w 
        self.col = col 
    def __lt__(self, other) : 
        return self.w > other.w # 按w从大到小排序
        
N = int(input()) 
lst = [] 

for i in range(N) : 
    w, col = input().split()
    lst.append(Node(int(w), col))

lst.sort() # 排序

for i in range(N) : 
    print(lst[i].col)

使用优先队列的写法

from queue import PriorityQueue 

class Node : 
    def __init__(self, w, col) : 
        self.w = w 
        self.col = col 
    def __lt__(self, other) : 
        return self.w > other.w 
        
q = PriorityQueue() 
n = int(input())

for i in range(n) : 
    w, col = input().split() 
    w = int(w)
    q.put(Node(w, col)) 
    
while q.qsize() : 
    print(q.get().col)

猜你喜欢

转载自blog.csdn.net/m0_54689021/article/details/130352281
今日推荐