【问题标题】:Python: deque out of index, why?Python:双端队列超出索引,为什么?
【发布时间】:2020-11-08 15:49:56
【问题描述】:

我有一个 ID 为字符串格式 (list_id) 的列表。我创建了一个数字从 1 到 list_id 长度的双端队列 (de_list_id),这样我就可以引用 list_id 的每个元素。 此外,我还有一个功能可以检查文件(带有特定 ID)是否在线备份。我希望循环重复,直到一切都在线。我写道:

while de_list_id:
    k = de_list_id.popleft()  #take the first element and check if it is online
    status = check(arg1, arg2, list_id[de_list_id[k]], arg3)

    if status:
        dl_routine(arg1, arg2, list_id[de_list_id[k]], arg3)  # if so, download and remove
    else:
       de_list_id.append(k)  # if not, append it back to list

但是返回:

IndexError:双端队列索引超出范围

有谁知道为什么以及如何解决这个问题?任何帮助将非常感激。提前致谢!

【问题讨论】:

  • 你的代码缩进真的像你的问题,if 块在循环之外吗?
  • 抱歉,当然不是。我编辑了。

标签: python while-loop deque


【解决方案1】:

我怀疑这就是问题所在:

k = de_list_id.popleft()  
status = check(arg1, arg2, list_id[de_list_id[k]], arg3)

你从队列中弹出索引k,然后用它来选择队列本身的索引和de_list_id[k]

根据你的描述,我认为你想做这样的事情:

k = de_list_id.popleft()  
status = check(arg1, arg2, list_id[k], arg3)

dl_routine(arg1, arg2, list_id[de_list_id[k]], arg3) -> dl_routine(arg1, arg2, list_id[k], arg3) 行相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-19
    • 2018-09-08
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多