slice() <Python 内置函数>

作者 : 开心源码 本文共2343个字,预计阅读时间需要6分钟 发布时间: 2022-05-12 共155人阅读

转载须注明出处:简书@Orca_J35 | GitHub@orca-j35,所有笔记均托管于 python_notes 仓库

相关笔记:『Subscriptions & Slicings.md』

假如需要生成迭代器版本的切片,则可使用 itertools.islice() 方法。

?? class slice(stop)

?? class slice(start, stop[, step])

该函数用于创立切片(slice)对象,切片对象表示由 range(start, stop,step) 指定的索引的集合。

start, stop, step 可以是任意类型的值,其中 start 和 step 的默认值是 None。

slice(6,b'a','b') #> slice(6, b'a', 'b')slice(6) #> slice(None, 6, None)

在使用扩展索引语法时,会自动生成切片对象。例如 a[start:stop:step] 将被翻译为 a[slice(start,stop,step)] ,并使用 None 填充切片中缺少的项,而后将结果传递给 __getitem__ 方法。因而,在使用扩展索引语法时,也可直接使用切片对象。

class Cls:    def __getitem__(self,key):        return key# 使用扩展索引语法时,会自动生成切片对象,还会使用None填充空缺项Cls()[0:1] #> slice(0, 1, None)a = [1,2,3,4,5,6,7,8,9]a[0:7:2] #> [1, 3, 5, 7]# 与直接使用切片对象等效a[slice(0,7,2)] #> [1, 3, 5, 7]

示例 – 为自己设置类实现 subscription 和 slicing:

# 该示例没有实现负索引,也没有对切片的step进行解决class Fib(object):    def __getitem__(self, n):        # 需要手动判断实参类型,从而区分subscription和slicing        if isinstance(n, int): # n是索引            a, b = 1, 1            for x in range(n):                a, b = b, a + b            return a        if isinstance(n, slice): # n是切片            # 展现如何利用切片对象完成切片操作            start = n.start # 获取切片的下界            stop = n.stop # 获取切片的上界            if start is None:                start = 0            a, b = 1, 1            L = []            for x in range(stop):                if x >= start:                    L.append(a)                a, b = b, a + b            return LFib()[:5] #> [1, 1, 2, 3, 5]# 可直接使用 slice 对象Fib()[slice(10)] #> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Slice objects

本节涵盖了 The standard type hierarchy -> Slice objects 中的内容。

切片对象用于表示 __getitem__() 方法取得的切片,也可通过内置函数 slice() 创立切片对象。

切片对象拥有三个特殊只读属性:

  • start is the lower bound;
  • stop is the upper bound;
  • stepis the step value;

以上三个属性可以是任意类型的值,并且没有其余功能,但是 Numerical Python 和其它第三方扩展会使用这三个属性。假如在创立切片对象时,省略了上述某个属性,则会将其设置为 None。

class Cls:    def __getitem__(self,key):        return keyCls()[0,b'orca':'_']#> (0, slice(b'orca', '_', None))

在使用扩展索引语法时,会自动生成切片对象。例如 a[start:stop:step] 将被翻译为 a[slice(start,stop,step)] ,并使用 None 填充切片中缺少的项。在使用扩展索引语法时,也可直接使用切片对象。

class Cls:    def __getitem__(self,key):        return key# 使用扩展索引语法时,会自动生成切片对象,还会使用None填充空缺项Cls()[0:1] #> slice(0, 1, None)a = [1,2,3,4,5,6,7,8,9]a[0:7:2] #> [1, 3, 5, 7]# 直接使用切片对象a[slice(0,7,2)] #> [1, 3, 5, 7]

切片对象支持一种方法:

  • slice.indices(self, length)

    This method takes a single integer argument length and computes information about the slice that the slice object would describe if applied to a sequence of length items. It returns a tuple of three integers; respectively these are the start and stop indices and the step or stride length of the slice. Missing or out-of-bounds indices are handled in a manner consistent with regular slices.

    # S.indices(len) -> (start, stop, stride)# 该方法用于计算,将长度为length的序列按照slice对象执行切片时,# 子序列在原序列中的(start, stop, step)slice(3).indices(4) #> (0, 3, 1)slice(3).indices(2) #> (0, 2, 1)

由于 slice.__hash__ 等于 None,所以当试图获取 slice 类的实例的哈希值时会抛出异常。

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » slice() <Python 内置函数>

发表回复