Python内置函数(29)——slice
英文文档:
- 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 steparguments default toNone. Slice objects have read-only data attributesstart,stopandstepwhich 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]ora[start:stop, i]. Seeitertools.islice()for an alternate version that returns an iterator.
根据传入的参数生成一个可切片对象
说明:
1. 函数实际上是一个切片类的构造函数,返回一个切片对象。
2. 切片对象由3个属性start、stop、step组成,start和step默认值为None。切片对象主要用于对序列对象进行切片取对应元素。
>>> help(slice)
class slice(object)
| slice(stop)
| slice(start, stop[, step])
|
| Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).
|
| Methods defined here:
|
| ...#省略#
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| start
|
| step
|
| stop
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a[None:5:None] # start step显式为None
[0, 1, 2, 3, 4]
>>> a[:5:] # start step默认为None
[0, 1, 2, 3, 4]
>>> a[2:5:None] # step显式为None
[2, 3, 4]
>>> a[2:5:] # step默认为None
[2, 3, 4]
>>> a[1:10:3]
[1, 4, 7]
3. 对应切片对象的3个属性start、stop、step,slice函数也有3个对应的参数start、stop、step,其值分别会付给切片对象的start、stop、step。
>>> c1 = slice(5) # 定义c1
>>> c1
slice(None, 5, None)
>>> c2 = slice(2,5) # 定义c2
>>> c2
slice(2, 5, None)
>>> c3 = slice(1,10,3) # 定义c3
>>> c3
slice(1, 10, 3)
>>> a[c1] # 和a[:5:]结果相同
[0, 1, 2, 3, 4]
>>> a[c2] # 和a[2:5:]结果相同
[2, 3, 4]
>>> a[c3] # 和a[1:10:3]结果相同
[1, 4, 7]
Python内置函数(29)——slice的更多相关文章
- Python内置函数(29)——help
英文文档: help([object]) Invoke the built-in help system. (This function is intended for interactive use ...
- Python内置函数(58)——slice
英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...
- Python内置函数(19)-slice
官方文档 class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set o ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
随机推荐
- 'net’ 不是内部命令或外部命令,也不是可运行的程序或批处理文件
我的电脑-->属性-->高级-->环境变量 path的变量值新加: %SystemRoot%\system32 修改完成后,需要重新打开cmd命令行,否则不会生效的.
- log4net应用实践(一)
1.背景 log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等)的工具.它是.Net下一 ...
- python 全栈开发,Day3(正式)
一.基础数据类型 基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余 + - * / ** %...2.bool 布尔值 判断真假以及作为条件变量3. ...
- Cesium home键定位的位置
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(80, 22, 130, 50);//home定位到中国范围
- Learn Plan
2018-02-05 1.正则表达式的熟悉和使用 2.Spring-boot 框架的使用 3.React的如何和使用 4.英语的单词发音的纠正和词汇的记忆. 5.github 命令行的使用 6.jav ...
- anaconda下安装新包一直报错(‘parse() got an unexpected keyword argument 'transport_encoding'’)
pip没有更新 解决方案:在prompt上输入 " conda install pip "
- JDK1.8源码(四)——java.util.Arrays 类
java.util.Arrays 类是 JDK 提供的一个工具类,用来处理数组的各种方法,而且每个方法基本上都是静态方法,能直接通过类名Arrays调用. 1.asList public static ...
- ansible之二:模块用法
一:ansible远程执行命令 [root@ansible ~]# ansible test -m shell -a "date" >> 2016年 08月 02日 星 ...
- SpringAOP-JDK 动态代理和 CGLIB 代理
在 Spring 中 AOP 代理使用 JDK 动态代理和 CGLIB 代理来实现,默认如果目标对象是接口,则使用 JDK 动态代理,否则使用 CGLIB 来生成代理类. 1.JDK 动态代理 那么接 ...
- android中include标签使用详解
android中include标签是为了便于控件的覆用的一个很好解决方案. 但是也有一些需要注意的地方,下面是本人在项目中碰到过的一个问题,做此记录,便于以后查看. include标签用法. ...