python - 在迭代列表时从列表中删除项目
问题描述
Python 脚本中有两个单独的进程在运行。两者都与全局变量交互POST_QUEUE = []
- 进程 1 (
P1
)POST_QUEUE
每 60 秒添加一次项目。一次可以是 0 到 50 个项目。 - 进程 2 (
P2
) 以设定的时间间隔通过 for 循环进行迭代,POST_QUEUE
并一次对列表项执行一项操作。在执行所述操作之后,该过程从列表中移除该项目。
以下是 的通用版本P2
:
def Process_2():
for post in POST_QUEUE:
if perform_operation(post):
Print("Success!")
else:
Print("Failure.")
POST_QUEUE.remove(post)
可以理解的是,我遇到了一个问题,当从 for 循环正在迭代的列表中删除项目时,它会破坏索引并比预期更早终止循环(即,在它对每个帖子执行必要的操作之前和将其从POST_QUEUE
) 中删除。
有没有更好的方法来做到这一点,而不是在从原始对象中删除项目时创建一个副本POST_QUEUE
并对其进行P2
迭代?例如:POST_QUEUE
def Process_2():
POST_QUEUE_COPY = POST_QUEUE[:]
for post in POST_QUEUE_COPY:
if perform_operation(post):
Print("Success!")
else:
Print("Failure.")
POST_QUEUE.remove(post)
解决方案
由于您实际上并不需要元素的索引,因此我建议您将这样的事情作为一个简单的解决方案:
def Process_2():
while len(POST_QUEUE):
if perform_operation(post[0]):
Print("Success!")
else:
Print("Failure.")
POST_QUEUE.remove(post[0])
但是,此解决方案的每次使用循环的运行时间为 O(n^2),因为 python 需要在每次迭代时移动列表中的每个元素。
因此,IMO 更好的实施方式是:
def Process_2():
reversed_post_queue = POST_QUEUE[::-1]
while len(reversed_post_queue):
if perform_operation(post[-1]):
Print("Success!")
else:
Print("Failure.")
POST_QUEUE.remove(post[-1])
这样你就可以保持顺序(我认为这在整个答案中对你很重要),同时只移动列表的元素一次并导致运行时间为 O(n)
最后,IMO 的最佳实现是创建或导入队列模块,以便您可以轻松地将列表用作 FIFO。
推荐阅读
- javascript - Slick Slide 移除了响应式断点上的内联样式
- r - 在 purrr 循环中选择非缺失变量
- javascript - Array.splice 有效,Lodash.remove 无效
- php - PHP没有给出错误并且不会将项目填充到数据库中
- javascript - 物联网:如何计算云物联网平台中 API 的时间复杂度
- android - 从 Firestore 在 android studio 上将时区设置为伦敦
- ms-access - 更改通过 VBA 生成的 Outlook 电子邮件签名的字体大小
- python - 如何在 python3 脚本的 smb 共享文件夹中保存文件
- typescript - 尽管有类型保护,Typescript 仍会引发“对象可能为‘null’”错误
- python - 在列表元组中成对添加元素