如果不适合对字符串数据进行字母数字排序,只需将元组的第二项用作次要优先级。当您有多个具有相同优先级的项目时,日期/时间优先级将为您提供一个优先级队列,该队列回退到 FIFIO 队列。这是一些仅具有次要数字优先级的示例代码。在第二个位置使用日期时间值是一个非常微不足道的更改,但如果您无法使其正常工作,请随时在 cmets 中戳我。
代码
import Queue as queue
prio_queue = queue.PriorityQueue()
prio_queue.put((2, 8, 'super blah'))
prio_queue.put((1, 4, 'Some thing'))
prio_queue.put((1, 3, 'This thing would come after Some Thing if we sorted by this text entry'))
prio_queue.put((5, 1, 'blah'))
while not prio_queue.empty():
item = prio_queue.get()
print('%s.%s - %s' % item)
输出
1.3 - This thing would come after Some Thing if we didn't add a secondary priority
1.4 - Some thing
2.8 - super blah
5.1 - blah
编辑
如果您使用时间戳来伪造 FIFO 作为次要优先级,使用日期如下所示。我说假是因为它只是大约 FIFO,因为在时间上非常接近添加的条目可能不会完全 FIFO。我添加了一个短暂的睡眠,所以这个简单的例子以合理的方式工作。希望这有助于作为另一个示例,说明您如何获得所需的订单。
import Queue as queue
import time
prio_queue = queue.PriorityQueue()
prio_queue.put((2, time.time(), 'super blah'))
time.sleep(0.1)
prio_queue.put((1, time.time(), 'This thing would come after Some Thing if we sorted by this text entry'))
time.sleep(0.1)
prio_queue.put((1, time.time(), 'Some thing'))
time.sleep(0.1)
prio_queue.put((5, time.time(), 'blah'))
while not prio_queue.empty():
item = prio_queue.get()
print('%s.%s - %s' % item)