python步长是什么

美女程序员鼓励师

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步长是什么 的全部内容, 来源链接: utcz.com/z/543908.html

回到顶部