可切片条件
线性结构p 可迭代 for … inp len()可以获取长度
通过下标可以访问
可以切片p 学过的线性结构
列表、元组、字符串、bytes、bytearr
切片 前包后不包
[ m=<a<n )
通过索引区间访问线性结构的一段数据
sequence[start:stop] 表示返回[start, stop)区间的子序列
支持负索引
start为0,可以省略
stop为末尾,可以省略
超过上界(右边界),就取到末尾;超过下界(左边界),取到开头
start一定要在stop的左边
[:] 表示从头至尾,全部元素被取出,等效于copy()方法
list('www.hello.com')
['w', 'w', 'w', '.', 'h', 'e', 'l', 'l', 'o', '.', 'c', 'o', 'm']
list('www.hello.com')[4:9]
['h', 'e', 'l', 'l', 'o']
步长切片
[start:stop:step]
step为步长,可以正、负整数,默认是1
step要和start:stop同向,否则返回空
list('www.hello.com')[4:9:2]
['h', 'l', 'o']
list('www.hello.com')[8:4:-2]
['o', 'l']
list('www.hello.com')[::-2]
['m', 'c', 'o', 'l', 'h', 'w', 'w']