我有一个项目列表:list = [a, b, c, d] 我想修改每个项目,然后修改 time.sleep(10),然后再进行下一次迭代。在最后一次迭代中,我想修改该项目,但要避免休眠。
for item in list:
# modify item
time.sleep(10) # avoid this line on the final iteration
不必是这种结构——寻找任何可以完成工作的方法。
请您参考如下方法:
您可以先 sleep ,但在第一次迭代 sleep 时跳过 sleep ,而是:
for idx, item in enumerate(list):
if idx != 0:
time.sleep(10) # avoid this line on the final iteration
# modify item
它在最后一个项目上掉下来,没有 sleep 。
