Python - 再次循环最后一个元素

Pho*_*nix 0 python loops python-3.3

a=[1,2,3,4]

for i in a:
    if someConditon:
        print(i)
    else:
        loop over last element again
Run Code Online (Sandbox Code Playgroud)

我正在使用selenium与网页进行交互并下载pdf文档.有时在下载过程中会发生错误,并且文件不会被保存.文件的实际保存存在于for循环中,我想添加一个条件,如果发现为false,则在尝试成功下载项目时再次循环同一元素.

我的问题是:如何让python再次循环遍历同一个元素

And*_*ark 6

没有办法告诉for循环没有推进迭代器,所以你需要使用while循环并手动增加i,或者在for循环体内执行任何其他循环.我是这样写的:

for i in a:
    while not someCondition:
        # do something
    print(i)
Run Code Online (Sandbox Code Playgroud)