开始索引以迭代Python列表 [英] Start index for iterating Python list

查看:70
本文介绍了开始索引以迭代Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中迭代列表时,设置起始索引的最佳方法是什么.例如,我有一个星期几的列表-星期日,星期一,星期二,...星期六-但我想从星期一开始遍历该清单.最佳做法是什么?

What is the best way to set a start index when iterating a list in Python. For example, I have a list of the days of the week - Sunday, Monday, Tuesday, ... Saturday - but I want to iterate through the list starting at Monday. What is the best practice for doing this?

推荐答案

您可以使用切片:

for item in some_list[2:]:
    # do stuff

这将从第三个元素开始,并迭代到结束.

This will start at the third element and iterate to the end.

这篇关于开始索引以迭代Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆