python:for循环从list列表的第二个元素开始遍历

菜鸟教程(list列表):https://www.runoob.com/python/python-lists.html

1. list列表的切片:

  • 切片语法:[起始:结束:步长],步长默认为1,可为负数,切片区间左闭右开

2. 代码示例

  • 一般元素是从0开始计数,所以将起始设为1,就是从第2个元素开始遍历
str_centence_list = [ "apple", "banana", "pear", "grape"]


for s_str in str_centence_list[1:]:    # python 从list列表的第2个元素开始遍历
    print(s_str)


'''
输出结果为:
banana
pear
grape
'''

参考:

https://blog.csdn.net/qq_29720657/article/details/103995335  python for循环从第二个元素开始遍历

猜你喜欢

转载自blog.csdn.net/weixin_39450145/article/details/120008234
今日推荐