Question
I need a good explanation (references are a plus) on Python's slice notation. To me, this notation needs a bit of picking up.
It looks extremely powerful, but I haven't quite got my head around it.
How-To
It's pretty simple really:
- a[start:end] # items start through end-1
- a[start:] # items start through the rest of the array
- a[:end] # items from the beginning through end-1
- a[:] # a copy of the whole array
- a[start:end:step] # start through not past end, by step
- a[-1] # last item in the array
- a[-2:] # last two items in the array
- a[:-2] # everything except the last two items
- a[::-1] # all items in the array, reversed
- a[1::-1] # the first two items, reversed
- a[:-3:-1] # the last two items, reversed
- a[-3::-1] # everything except the last two items, reversed
Supplement
* What does list[x::y] do? [duplicate]
沒有留言:
張貼留言