In Python, the more code is better, the less the better. The more complex the code is, the better it is, but the simpler the better.
List Slice
>>> L = ['Michael','Sarah','Tracy','Bob','Jack']>>> L[0:3]['Michael','Sarah','Tracy']
#如果索引以0开始
>>> L[:3]
[' Michael ', ' Sarah ', ' Tracy ']
Inverted slices
>>> l[-2:] #倒数几个就是负几 ['Bob'Jack ' ]>>> l[-2:-1['Bob']
Example:
1: Traverse out 1-100
>>> L = list (range (100))
2: Take out 1-10-bit, how many bits is l[:10]
>>> l[:101, 2, 3, 4, 5, 6, 7, 8, 9]
3:10 digits after removal
>>> l[-10: [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
4: Remove 11-20-bit
>>>l[10:20[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
5: First 10 numbers, each 5 take one
>>>l[:10:2]# back [2,4,6,8]
6: All numbers per 5 take one
>>>l[::55, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
7: Copy List
>>>1, 2, 3, ..., 99]
Tuple slices
>>> (1,2,3,4,5,6) [: 3] (1, 2, 3)
' xxx ' can also be seen as a list
' ABCDEFG ' [: 3] ' ABC ' ' ABCDEFG ' [:: 2] ' Aceg '
Python Advanced Features-slicing