官方文档

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. git点滴

    git指定版本,SHA-1短的,长的都可以 git checkout c66a9be git checkout c66a9befsadf1sdf1s3fd21 git log ##查询本地log gi ...

  2. root登录

    ,编辑/etc/lightdm/lightdm.conf: gedit /etc/lightdm/lightdm.conf [Seat:*] autologin-guest=false autolog ...

  3. Oracle 11g 详细安装步骤

    一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地址: http://www.ora ...

  4. man mkfs

    ---恢复内容开始--- MKFS(8)                                                                MKFS(8) NAME/名称  ...

  5. Cobaltstrike系列教程(一)-简介与安装

    0x001-Cobaltstrike简介 Cobalt Strike是一款美国Red Team开发的渗透测试神器,常被业界人称为CS.这款神器许多大佬们都已经玩的很6,我一个菜鸡玩的略有心得,因此写一 ...

  6. 对vue的api的研究

    Vue.config 是一个对象,包含 Vue 的全局配置.可以在启动应用之前修改下列属性: silent 类型:boolean 默认值:false 用法: Vue.config.silent = t ...

  7. [CSP-S模拟测试]:Divisors(数学)

    题目描述 给定$m$个不同的正整数$a_1,a_2,...,a_m$,请对$0$到$m$每一个$k$计算,在区间$[1,n]$里有多少正整数是$a$中恰好$k$个数的约数. 输入格式 第一行包含两个正 ...

  8. 神奇的hsl

    HSL色彩模式是工业界的一种颜色标准,是通过对色相(H).饱和度(S).明度(L)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的.今天我们看看这种色彩模式,能在CSS中产生什么神奇的变 ...

  9. php面试专题---13、AJAX基础内容考点

    php面试专题---13.AJAX基础内容考点 一.总结 一句话总结: ajax对提升用户速度,缓解服务器压力方面也是很有可取之处的,毕竟传递的数据少了 1.AJAX基础概念? Asynchronou ...

  10. jenkins展示html测试报告(不使用html publisher)

     最终效果: 点击『测试报告』,查看测试报告: 一开始的思路是: jenkins上展示html报告[转载] jenkins使用HTML Publisher Plugin插件 拉取报告样式缺失问题解决 ...