Python内置函数(19)-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 step arguments 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) | 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的更多相关文章
- Python内置函数(19)——eval
英文文档: eval(expression, globals=None, locals=None) The arguments are a string and optional globals an ...
- Python内置函数(29)——slice
英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...
- Python内置函数(58)——slice
英文文档: class slice(stop) class slice(start, stop[, step]) Return a slice object representing the set ...
- Python内置函数(19)——oct
英文文档: oct(x) Convert an integer number to an octal string. The result is a valid Python expression. ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- Python之路Python内置函数、zip()、max()、min()
Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...
- python内置函数简单归纳
做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
随机推荐
- 如何在centos7中设置redis服务器开机自启动
1.简单说明centos7系统中有不同类型的程序,一类是操作系统的服务程序,另一类是第三方程序,而redis就是第三方程序,每次关机后开机都要手工重新启动,很麻烦,那么如何把redis设置为开机自启动 ...
- 项目中dubbo的标准配置
# Spring boot applicationspring: application: name: hello-dubbo-service-user-provider # UserService ...
- iconfont的三种使用方式
这篇文章主要介绍了iconfont的三种使用方式,需要的朋友可以参考下 在我们项目中经常要使用到iconfont,在此我们使用阿里巴巴矢量库提供的icon图标,此图标库足够为我们提供大量的图标,我 ...
- CF286E Ladies' Shop FFT
题目链接 读完题后,我们发现如下性质: 在合法且和不超过 $m$ 的情况下,如果 $a_{i}$ 出现,则 $a_{i}$ 的倍数也必出现. 所以如果合法,只要对所有数两两结合一次就能得到所有 $a_ ...
- 修改Oracle数据库SGA和PGA大小
SGA的大小:一般物理内存20%用作操作系统保留,其他80%用于数据库.SGA普通数据库可以分配40%-60%之间,PGA可以分配20%-40%之间.1.以dba身份登录并查看SGA信息:SQL> ...
- php str_replace与substr_replace的区别
函数定义: str_replace() :函数替换字符串中的一些字符(区分大小写). substr_replace() :函数把字符串的一部分替换为另一个字符串. 区别: str_replace()和 ...
- xpath的几个常用规则
我们在定位页面元素的时候呢,经常使用到xpath.xpah定位元素,我们可以使用开发者工具,然后右键选取元素的xpath ,但是这种方式得到的xpath是绝对路径,如果页面元素发生变动,经常会出现定位 ...
- 用maven给SpringBoot项目打包
注意要点: 1.注意某个moule有依赖需要在对应的pom.xml里填写有关的信息,如: <dependencies> <dependency> <artifactId& ...
- 【SpringBoot】 Java中如何封装Http请求,以及JSON多层嵌套解析
前言 本文中的内容其实严格来说不算springboot里面的特性,属于JAVA基础,只是我在项目中遇到了,特归纳总结一下. HTTP请求封装 目前JAVA对于HTTP封装主要有三种方式: 1. JAV ...
- linux-批量修改目录下后缀shell
#!/bin/bashcd /optrename .sh .shell *.shecho "后缀修改成功"