Python 数组的冒号作用
It’s pretty simple really:
a[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array
也就是说:
a[1:5] is equivalent to "from 1 to 5" (5 not included)
a[1:] is equivalent to "1 to end"
a[len(a):] is equivalent to "from length of a to end"
There is also the step
value, which can be used with any of the above:
a[start:stop:step] # start through not past stop, by step
The key point to remember is that the :stop
value represents the first value that is not in the selected slice. So, the difference between stop
and start
is the number of elements selected (if step
is 1, the default).
参考链接:
https://*.com/questions/509211/understanding-slice-notation
https://*.com/questions/4012340/colon-in-python-list-index