0

我得到了我实现的任务类,我想将我所有的任务插入优先级队列,python中有一个吗?我是否需要在我的类中实现一些 func 以使其具有可比性?

每个班级都有优先级。

4

1 回答 1

1

There is indeed a priority queue in Python, see here: https://docs.python.org/3/library/queue.html#Queue.PriorityQueue

Here is a simple example:

from queue import PriorityQueue

q = PriorityQueue()

q.put((2, 'code'))
q.put((1, 'eat'))
q.put((3, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

# Result:
#   (1, 'eat')
#   (2, 'code')
#   (3, 'sleep')

You can also use heapq as well.

Can't comment on your implementation without knowing more information...

于 2019-07-17T20:19:46.610 回答