python步长是什么

562次阅读
没有评论

python步长是什么

python步长概念

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

python步长判断

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

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

python步长实例

列表重复步长删除元素。

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切片步长怎样实现

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

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-29发表,共计644字。
新手QQ群:570568346,欢迎进群讨论 Python51学习