切片
切片就是切取片段的意思吧!
切片常见对象
listtuplestring
切片格式
<对象>[x:y:z]
- 当
x、y、z当中有一个不写,表示使用默认值:z的默认值为1,x、y的默认值分别是0和len(<对象>),y位置的值不取
常见切片用法
- 取
[0,n)的数据
>>> l=['peryter',90,'momo','huo','amiee']
>>> l[:4]
['peryter', 90, 'momo', 'huo']
- 取
n之后的数据
>>> l[4:]
['amiee']
- 取
[m,n)的数据
>>> l[2:4]
['momo', 'huo']
- 按步长取
[m,n)的数据
>>> s='abcdefghijklmn'
>>> s[::3]
'adgjm'











网友评论