• 技术文章 >Python技术 >Python基础教程

    python步长是什么

    小妮浅浅小妮浅浅2021-03-18 11:23:37原创13089

    1、概念

    步长是切片里的step,step不能为0,默认为1。

    2、步长判断

    若 step > 0, 则示意从左向右举行切片。此时,start必需小于end才有效果,否则为空。比方: s[0,: 5: 2]的效果是’ace’。

    若 step < 0, 则示意从右向左举行切片。 此时,start必需大于end才有效果,否则为空。列如: s[5: 0: -1]的效果是’fedcb’。

    3、实例

    列表重复步长删除元素。

    def last_item(lt, step):
        while len(lt) >= step and step != 1:
            lt.pop(step - 1)
            # print(lt)
            lt = lt[step - 1:] + lt[:step - 1]
        while len(lt) < step and len(lt) != 1:
            n = step % len(lt)
            lt.pop(n - 1)
        else:
            if step == 1:
                return (lt[-1])
            else:
                return lt[0]```

    以上就是python步长的介绍,本篇涉及到了有关切片的知识点,大家如果对这部分有所遗忘,可以重新复习一下。更多Python学习指路:python基础教程

    (推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

    专题推荐:python步长
    品易云
    上一篇:python try是什么意思 下一篇:python环境变量是什么意思

    相关文章推荐

    • python sys模块是什么• python gui是什么• python wrapper是什么• python try是什么意思• python中format是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网