天天看点

Python高级特性-切片

L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']

L[0:3]

表示,从索引

开始取,直到索引

3

为止,但不包括索引

3

。即索引

1

2

,正好是3个元素

print('L[0:3] =', L[0:3])

输出结果:L[:3] = ['Michael', 'Sarah', 'Tracy']

print('L[:3] =', L[:3])

输出结果:L[:3] = ['Michael', 'Sarah', 'Tracy']

print('L[1:3] =', L[1:3])

输出结果:L[1:3] = ['Sarah', 'Tracy']

print('L[-2:] =', L[-2:])

输出结果:L[-2:] = ['Bob', 'Jack']

----------------------------------------------------------

R = list(range(100))  ----创建一个0-99的数列

print('R[:10] =', R[:10])

输出结果:R[:10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print('R[-10:] =', R[-10:])

输出结果:R[-10:] = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

print('R[10:20] =', R[10:20])

输出结果:R[10:20] = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

print('R[:10:2] =', R[:10:2])

输出结果:R[:10:2] = [0, 2, 4, 6, 8]

print('R[::5] =', R[::5])

输出结果为:R[::5] = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]