python - 循环直到每个元素都返回 true
问题描述
我有一个带有 ids 的列表(列表)。作为使用 wget 进行在线检查的结果,该列表的每个元素都会返回一个字符串“true”或“false”。我想遍历该列表,只要有一个元素返回“假”值。基本上我想重复一下:
for i in range(len(list)):
wget online check
if status == 'true':
write id to another list
elif status == 'false':
continue
time.sleep()
一遍又一遍,直到一切都是真的。
我用嵌套的while循环尝试了它:
for j in range(len(list)):
while status_ == 'false':
wget online check
if status == 'true':
write id to another list
elif status == 'false':
continue
time.sleep()
但这不起作用。有人可以帮忙吗?
干杯
解决方案
使用 adeque
作为旋转队列,成功时从双端队列中删除一个值。只要deque
不为空,循环就会继续。
(双端队列就像一个列表,但您可以有效地向任一端添加元素或从中删除元素。)
from collections import deque
d = deque(list)
while d:
i = d.popleft()
wget online check
if status == "true":
write id to another list
else:
d.append(i) # Put it back to try again later
time.sleep(...)
推荐阅读
- python - 如何正确递归调用替代类型的二进制搜索(python)?
- javascript - 如何在赛普拉斯的 API 调用中使用变量作为参数
- python - 从 pymysql 更频繁地使用 connection.commit() 到 AWS RDS 是否更昂贵?
- javascript - 为什么这个 JS 代码在 altervista 上不起作用?
- flutter - 如何为 Flutter SliverList 中的元素设置动画?
- python - 基于 Unix 时间戳对自定义数据进行分箱
- typescript - 如何将我的打字稿代码分成多个文件?
- scala - scala.math.BigDecimal : 1.2 和 1.20 相等
- r - DT 数据表中单元格选择期间的行为不一致
- session - Wamp64不在页面之间保持会话,每次页面更改都会生成一个新会话