如何在 Python 的 for 循环中检索剩余项目?

do-*_*-me 2 python for-loop

我有一个简单的 for 循环迭代项目列表。在某些时候,我知道它会破裂。我怎样才能退回剩余的物品?

for i in [a,b,c,d,e,f,g]:
    try: 
        some_func(i)
    except:
        return(remaining_items) # if some_func fails i.e. for c I want to return [c,d,e,f,g]
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用我的初始列表并从每次迭代的开始逐一删除项目。但是是否有一些本机 Python 函数用于此或更优雅的功能?

Pac*_*ac0 5

您可以利用enumerate它在列表中生成元素及其索引。

myList = [a,b,c,d,e,f,g]
for index, item in enumerate(myList):
    try: 
        some_func(item)
    except:
        return myList[index:]
Run Code Online (Sandbox Code Playgroud)

在线测试