python中列表切片
python中列表切片。
1、基本用法
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[2:5]
[33, 44, 55]
2、
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[:3]
[11, 22, 33]
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[7:]
[88, 99, 0]
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[:]
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
3、
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test2[-5:-2]
[66, 77, 88]
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[-5:]
[66, 77, 88, 99, 0]
4、
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[0:10:2]
[11, 33, 55, 77, 99]
>>> test1[::2]
[11, 33, 55, 77, 99]
>>> test1[::3]
[11, 44, 77, 0]
5、
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[::-1]
[0, 99, 88, 77, 66, 55, 44, 33, 22, 11]
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[::-2]
[0, 88, 66, 44, 22]
6、
>>> test1
[11, 22, 33, 44, 55, 66, 77, 88, 99, 0]
>>> test1[-1:-8:-2]
[0, 88, 66, 44]