官方文档

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)

 |      Return repr(self).
|
| indices(...)
| S.indices(len) -> (start, stop, stride)
|
| Assuming a sequence of length len, calculate the start and stop
| indices, and the stride length of the extended slice described by
| S. Out of bounds indices are clipped in a manner consistent with the
| handling of normal slices.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| start
|
| step
|
| stop
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
li = [11,22,33,44,55,66,77]

ret1 = li[None:5:None]  #start,stem为None
ret2 = li[:5:] #同上
print(ret1,ret2)
#输出
#[11, 22, 33, 44, 55] [11, 22, 33, 44, 55] ret3 = li[2:5:None] #step为None
ret4 = li[2:5:]
print(ret3,ret4) #同上
#输出
#[33, 44, 55]
#[33, 44, 55] ret5 = li[1:6:3]
print(ret5)
#输出
#[22, 55]

  3.对应切片的3个属性start、stop、step,slice函数也有3个对应的参数start、stop、step,其值会直接赋给切片对象的start、stop、step

#定义c1
c1 = slice(5)
print(c1)
#输出
#slice(None, 5, None) #定义c2
c2 = slice(2,5)
print(c2)
#输出
#slice(2, 5, None) #定义c3
c3 = slice(1,6,3)
print(c3)
#输出
#slice(1, 6, 3) a = list(range(1,10))
ret1 = a[c1] #同a[:5:]一样
print(ret1)
#输出
#[1, 2, 3, 4, 5] ret2 = a[c2] #同a[2:5:]一样
print(ret2)
#输出
#[3, 4, 5] ret3 = a[c3] #同a[1:6:3]一样
print(ret3)
#输出
#[2, 5]

Python内置函数(19)-slice的更多相关文章

  1. Python内置函数(19)——eval

    英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...

  2. Python内置函数(29)——slice

    英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...

  3. Python内置函数(58)——slice

    英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...

  4. Python内置函数(19)——oct

    英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...

  5. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  6. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  7. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  8. python内置函数简单归纳

    做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...

  9. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

随机推荐

  1. Vue的自定义滚动,我用el-scrollbar

    弄了一个持续更新的github笔记,可以去看看,诚意之作(本来就是写给自己看的--)链接地址:Front-End-Basics 此篇文章的地址:Vue的自定义滚动,我用el-scrollbar 基础笔 ...

  2. 前端每日实战:45# 视频演示如何用纯 CSS 创作一个菱形 loader 动画

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKzjqK 可交互视频教程 此视频 ...

  3. Map和Set的联系

    Java中的集合 Java中的集合包括三大类,它们是Set.List和Map,它们都处于java.util包中,Set.List和Map都是接口,它们有各自的实现类.Set的实现类主要有HashSet ...

  4. hashlib模块subprocess模块

    '''通过一种算法,将字符串得出一种编码内容相同则hash运算结果相同,内容稍微改变则hash值改变不可逆推相同算法,无论校验多长的数据,得到的hash值长度固定'''# import hashlib ...

  5. jmeter进阶之Beanshell引用Java代码

    在Jmeter实际的使用中,根据业务往往需要构造一些数据源,比如虚拟用户的账号信息等,为了快速灵活,较好的方式往往是通过代码来辅助生产数据. 故在Jmeter支持的众多 sampler 或 前/后置处 ...

  6. NotSerializableException

    这个错误是实体类没有  继承  Serializeble.

  7. Java 迭代器删除元素ConcurrentModificationException异常。

    Java是不支持容器类在使用迭代器迭代过程中,使用如 list.remove(obj)方法删除元素.否则会抛出ava.util.ConcurrentModificationException异常.应该 ...

  8. 如何把word文档内容和图片直接导入到wordpress编辑器

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

  9. 【转】django 正则URL 匹配

    django 正则URL 匹配  转自:https://www.cnblogs.com/chenkeven/articles/9305260.html 一.引子 在day17 作业中,我们查看主机详细 ...

  10. sql select语句详解

    先group by 后  order by SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]    * | expression [ AS ...