• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    python内置函数之slice案例详解
    POST TIME:2021-10-18 12:57

    英文文档:

    class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default). They have no other explicit functionality; however they are used by Numerical Python and other third party extensions. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i]. See itertools.islice() for an alternate version that returns an iterator.

    说明:

      1. 函数实际上是一个切片类的构造函数,返回一个切片对象。

      2. 切片对象由3个属性start、stop、step组成,start和step默认值为None。切片对象主要用于对序列对象进行切片取对应元素。

    >>> help(slice)
    class slice(object)
     |  slice(stop)
     |  slice(start, stop[, step])
     |  
     |  Create a slice object.  This is used for extended slicing (e.g. a[0:10:2]).
     |  
     |  Methods defined here:
     |  
     |  ...#省略#
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  start
     |  
     |  step
     |  
     |  stop
     |  
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |  
     |  __hash__ = None
    >>> a = list(range(10))
    >>> a
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    >>> a[None:5:None] # start step显式为None
    [0, 1, 2, 3, 4]
    >>> a[:5:] # start step默认为None
    [0, 1, 2, 3, 4]
    >>> a[2:5:None] # step显式为None
    [2, 3, 4]
    >>> a[2:5:] # step默认为None
    [2, 3, 4]
    >>> a[1:10:3]
    [1, 4, 7]

      3. 对应切片对象的3个属性start、stop、step,slice函数也有3个对应的参数start、stop、step,其值分别会付给切片对象的start、stop、step。

    >>> c1 = slice(5) # 定义c1
    >>> c1
    slice(None, 5, None)
    >>> c2 = slice(2,5) # 定义c2
    >>> c2
    slice(2, 5, None)
    >>> c3 = slice(1,10,3) # 定义c3
    >>> c3
    slice(1, 10, 3)
    >>> a[c1] # 和a[:5:]结果相同
    [0, 1, 2, 3, 4]
    >>> a[c2] # 和a[2:5:]结果相同
    [2, 3, 4]
    >>> a[c3] # 和a[1:10:3]结果相同
    [1, 4, 7]

    到此这篇关于python内置函数之slice案例详解的文章就介绍到这了,更多相关python内置函数之slice内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • Python:slice与indices的用法
    • Python高级特性切片(Slice)操作详解
    • python中slice参数过长的处理方法及实例
    • 详解python编程slice与indices函数用法示例
    上一篇:Python模型聚合查询\Q查询\F查询\分组查询操作技巧解析
    下一篇:详细介绍Python中的set集合
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信