Python slice是什么函数,用法是什么?
Admin 2022-01-11 群英技术资讯 1523 次浏览
篇文章我们来了解Python slice函数的相关内容,一些朋友可能对于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 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) 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 slice函数就介绍到这,上述示例具有一定的借鉴价值,感兴趣的朋友可以参考,希望能对大家有帮助,想要了解更多slice函数的使用,大家可以关注其它的相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章给大家带来了关于Python的相关知识,其中主要整理了随机森林模型的相关问题,包括了集成模型简介、随机森林模型基本原理、使用sklearn实现随机森林模型等等内容,
本文主要介绍了Python为什么要保留显式的self,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
常见的python文件的后缀名有哪些?一般我们比较常见的Python文件后缀有:py、pyc 、pyo、 pyi、pyw、 pyd、 pyx 等等。文本就给大家简单介绍下这些python文件的后缀名,感兴趣的朋友就继续往下看吧。
这篇文章主要为大家介绍了Python中的普通函数和高阶函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
三元表达式是编程中比较常用的,这篇文章就主要给大家分享python的三元表达式怎样用的操作,对新手学习python的三元表达式具有一定的借鉴价值,感兴趣的朋友可以参考一下,希望大家阅读完这篇文章能有所收获,下面我们一起来学习一下吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008