首页 > 解决方案 > 如何列出和更改 Python heapq 中的元素而不弹出它们?

问题描述

我有 Python 程序,它正在对一些任务进行排序并在一定延迟后执行它们。

请检查下面的代码

import heapq
import dateutil
from dateutil.parser import *
from datetime import *
from time import sleep
import time
h = []
now = parse("Sat Oct 11 17:13:46 UTC 2003")
today = now.date()
tup1=(dateutil.parser.parse('13:00 5.5.2018'), 10, 3, 'Job 1')
# push items into queue:
heapq.heappush(h, tup1)
heapq.heappush(h, (dateutil.parser.parse('14:00 5.5.2018'), 10, 2, 'Job 2'))
heapq.heappush(h, (dateutil.parser.parse('14:00 5.5.2018'), 1, 10, 'Job 3'))
heapq.heappush(h, (dateutil.parser.parse('13:30 5.5.2018'), 1, 3, 'Job 4'))

try:
    while True:
        if h:
            tuple=(heapq.heappop(h))
            print(int(tuple[2]))
            time.sleep(int(tuple[2]))

except KeyboardInterrupt:
    print "exiting"

如何从现有列表中修改某些内容,例如如果我想访问 Job 4 的日期值,以及如何在不弹出它们的情况下将它们全部列出?

标签: pythonheap

解决方案


推荐阅读