python - 如何列出和更改 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 的日期值,以及如何在不弹出它们的情况下将它们全部列出?
解决方案
推荐阅读
- python - 具有廉价梯度和已知最小值的函数的优化
- swift - 如何在 SwiftUI 中使列表单元格的背景透明?
- javascript - Javascript 为最终用户清除浏览器缓存
- compare - Octave如何有效地比较矩阵元素
- python - 在不创建 csv 本地文件的情况下将 pandas 数据框上传到 azure blob
- javascript - 在 chart.js 的 yaxis 上显示名称
- c# - ConnectionString 属性尚未初始化 将数据插入数据库时出错
- r - 通过遍历文件夹来更改标识符——R
- java - 带圆圈的项目符号 ⦿ 未出现在 API < 21 的 android WebView 上
- graphql - 如何在 GraphQL Nexus 的数组中定义非空元素?