Python中14个切片操作都有哪些
今天就跟大家聊聊有关Python中14个切片操作都有哪些,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
我们提供的服务有:网站建设、成都做网站、微信公众号开发、网站优化、网站认证、安陆ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的安陆网站制作公司
切片(Slice)是一个取部分元素的操作,是Python中特有的功能。它可以操作list、tuple、字符串。
Python的切片非常灵活,一行代码就可以实现很多行循环才能完成的操作。切片操作的三个参数 [start: stop: step] ,其中start是切片的起始位置,stop是切片的结束位置(不包括),step可以不提供,默认值是1,并且step可为负数(详细使用见下)
下面盘点下切片的常见操作。
1.切取的元素不包括右端点


2.切片左端点为0,可省略

3.每两个取一个

4.支持倒数切片,倒数第一个元素的索引是-1

5.原样复制一个list

6.起始索引和终止索引都省略,也就都为默认值,如下步长设置为2切取元素

7.一行代码实现列表反转

8.步长为负数,实际上指明了一个逆向迭代器。如下逆向倒数,每两个取一个。

9.值得注意,Python超出索引范围不报错

10.步长step为0,会报错

11.切片操作的三个参数可以用表达式

12.连续切片操作

13.元组对象的切片操作

14.字符串的切片

看完上述内容,你们对Python中14个切片操作都有哪些有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。
分享名称:Python中14个切片操作都有哪些
链接URL:http://myzitong.com/article/peeojp.html