享学课堂-快速掌握Python中的切片使用方法？

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[-1] # last item in the array

a[-2:] # last two items in the array

a[:-2] # everything except the last two items

a[start:end:step] # start through not past end, by step

+---+---+---+---+---+---+

| P | y | t | h | o | n |

+---+---+---+---+---+---+

0 1 2 3 4 5

-6 -5 -4 -3 -2 -1

>>> 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]]

• 发表于:
• 原文链接http://kuaibao.qq.com/s/20180322A0KKEO00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2020-09-21

2020-09-21

2020-09-21

2020-09-21

2020-09-21

2020-09-21