官方文档

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. Linux 普通用户自动修改密码

    在大量服务器运维中,维护服务器账号就让人头痛,对账号密码策略要求,现写了一个shell脚本来完成账号密码的修改,当然这个不是最好的方法,只是在没有其它辅助服务时使用,最好还是使用账户统一管理服务来维护 ...

  2. ltp-ddt eth_parallel_processing

    ETH_S_FUNC_PARALLEL_PROCESSING: source 'common.sh'; prepare_nfs_mount.sh "/mnt/nfs_mount"| ...

  3. 22.Express框架——2019年12月19日

    2019年12月19日14:16:36 1. express简介 1.1 介绍 Express框架是后台的Node框架,所以和jQuery.zepto.yui.bootstrap都不一个东西. Exp ...

  4. DGA域名检测

    一.DGA域名原理 僵尸网络(Botnet):互联网上在蠕虫.木马.后门工具等,传统恶意代码形态的基础上发展.融合而产生的一种新型攻击方法. DNS(Domain Name System) :基于 U ...

  5. Spring动态数据源-AbstractRoutingDataSource

    在分库分表的情况下,在执行SQL时选择连接不同的数据源(库)的思路:配置多个数据源加到动态数据源对象中,根据实际的情况动态切换到相应的数据源中. 如存放订单信息的有10个库,每个库中有100张表,根据 ...

  6. python学习笔记(十)常用模块

    import os print(os.getcwd())#取当前工作目录,绝对路径 print(os.chdir("../"))#更改当前目录,.代表当前目录,..代表上一级目录 ...

  7. Influxdb根据配置文件启动(Influxdb的数据存储)

    1.在Influxdb文件夹下建立一个bat文件 2.文件内容如下: @echo offSETLOCAL :: 获取当前批处理所在路径SET InfluxdP==%~dp0 :: 开启influxdb ...

  8. 20180826(04)-Java序列化

    Java序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信息和存储在对象中数据的类型 将序列化对象写入文件之后,可 ...

  9. OSI参考模型和网络排错

     OSI七层协议 应用层  应用程序通信服务 表示层  显示  加密  数据格式 会话层   服务器和客户机建立会话  netstat -nb 查看会话   mscofig 传输层 可靠回话传输 分段 ...

  10. bzoj 2002 Bounce 弹飞绵羊(分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 11202  Solved: 5698[Subm ...