首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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

方法1:

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

方法2:

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

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

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

0 1 2 3 4 5

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

方法3:

>>> 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
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券