Skip to main content
 首页 » 编程设计

python之我如何做一个 "for each",从列表的某个索引开始(Python)

2025年05月04日14shihaiming

假设我有这个列表:

thelist = ['apple','orange','banana','grapes'] 
for fruit in thelist: 

这会遍历所有的水果。

但是,如果我想从橙色开始怎么办?而不是从苹果开始? 当然,我可以做“如果...继续”,但一定有更好的方法吗?

请您参考如下方法:

for fruit in thelist[1:]: 
    ... 

这当然假设您知道从哪个索引开始。但您可以轻松找到索引:

for fruit in thelist[thelist.index('orange'):]: 
    ...