生成关于蛋糕的序列cake1:
In [1]: cake1 = list(range(5,0,-1)) In [2]: b = cake1[1:10:2] In [3]: b Out[3]: [4, 2] In [4]: cake1 Out[4]: [5, 4, 3, 2, 1]
再生成一个序列:
In [5]: from random import randint ...: cake2 = [randint(1,100) for _ in range(100)] ...: # 同样以间隔为2切前10个元素,得到切片d ...: d = cake2[1:10:2] In [6]: d Out[6]: [75, 33, 63, 93, 15]
你看,我们使用同一种切法,分别切开两个蛋糕cake1,cake2. 后来发现这种切法极为经典
,又拿它去切更多的容器对象。
那么,为什么不把这种切法封装为一个对象呢?于是就有了slice对象。
定义slice对象极为简单,如把上面的切法定义成slice对象:
perfect_cake_slice_way = slice(1,10,2) #去切cake1 cake1_slice = cake1[perfect_cake_slice_way] cake2_slice = cake2[perfect_cake_slice_way] In [11]: cake1_slice Out[11]: [4, 2] In [12]: cake2_slice Out[12]: [75, 33, 63, 93, 15]
与上面的结果一致。
对于逆向序列切片,slice
对象一样可行:
a = [1,3,5,7,9,0,3,5,7] a_ = a[5:1:-1] named_slice = slice(5,1,-1) a_slice = a[named_slice] In [14]: a_ Out[14]: [0, 9, 7, 5] In [15]: a_slice Out[15]: [0, 9, 7, 5]
频繁使用同一切片的操作可使用slice对象抽出来,复用的同时还能提高代码可读性。