[Python]数组切片
来源:https://stackoverflow.com/questions/509211/understanding-slice-notation
步长为正数
seq[:] # [seq[0], seq[1], ..., seq[-1] ] seq[low:] # [seq[low], seq[low+1], ..., seq[-1] ] seq[:high] # [seq[0], seq[1], ..., seq[high-1]] seq[low:high] # [seq[low], seq[low+1], ..., seq[high-1]] seq[::stride] # [seq[0], seq[stride], ..., seq[-1] ] seq[low::stride] # [seq[low], seq[low+stride], ..., seq[-1] ] seq[:high:stride] # [seq[0], seq[stride], ..., seq[high-1]] seq[low:high:stride] # [seq[low], seq[low+stride], ..., seq[high-1]]
步长为负数
seq[::-stride] # [seq[-1], seq[-1-stride], ..., seq[0] ] seq[high::-stride] # [seq[high], seq[high-stride], ..., seq[0] ] seq[:low:-stride] # [seq[-1], seq[-1-stride], ..., seq[low+1]] seq[high:low:-stride] # [seq[high], seq[high-stride], ..., seq[low+1]]
a[:] # a copy of the whole array 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
个性签名:做要做好,做到不三不四不如不做。