我对列表进行了循环:
MyList = ["test", "test2", "test3", "test4", "test5", "test6", "test7"]
for item in MyList:
print(item) # output: [test, test2, test3, test4, test5, test6, test7]
if item == "test7":
pass
现在在这里,如果
item == "test7"
我想回到“test3”并再次计数。
我怎样才能做到这一点?
我必须有一个
for
循环。我真的不能使用 while
循环。
喜欢这样吗? def Loop_and_return(lst, target_item, steps_back): 对于索引,枚举中的项目(lst): 打印(项目) 如果项目==目标项目: new_start_index = max(0,索引-steps_back) 对于 lst[new_start_index:] 中的 new_item: 打印(新项目)