Python里面的[::-1]和[::2]是什么意思

tech2022-09-25  103

Python里面的[::-1]和[::2]是什么意思

来源: https://docs.python.org/2.3/whatsnew/section-slices.html

L = range(10) L[::2] [0, 2, 4, 6, 8] L[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] s='abcd' s[::2] 'ac' s[::-1] 'dcba' 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
最新回复(0)