python內(nèi)置函數(shù)之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. 函數(shù)實(shí)際上是一個(gè)切片類的構(gòu)造函數(shù),返回一個(gè)切片對象。
2. 切片對象由3個(gè)屬性start、stop、step組成,start和step默認(rèn)值為None。切片對象主要用于對序列對象進(jìn)行切片取對應(yīng)元素。
>>> 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默認(rèn)為None [0, 1, 2, 3, 4] >>> a[2:5:None] # step顯式為None [2, 3, 4] >>> a[2:5:] # step默認(rèn)為None [2, 3, 4] >>> a[1:10:3] [1, 4, 7]
3. 對應(yīng)切片對象的3個(gè)屬性start、stop、step,slice函數(shù)也有3個(gè)對應(yīng)的參數(shù)start、stop、step,其值分別會(huì)付給切片對象的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:]結(jié)果相同 [0, 1, 2, 3, 4] >>> a[c2] # 和a[2:5:]結(jié)果相同 [2, 3, 4] >>> a[c3] # 和a[1:10:3]結(jié)果相同 [1, 4, 7]
到此這篇關(guān)于python內(nèi)置函數(shù)之slice案例詳解的文章就介紹到這了,更多相關(guān)python內(nèi)置函數(shù)之slice內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 定時(shí)器,輪詢定時(shí)器的實(shí)例
今天小編就為大家分享一篇python 定時(shí)器,輪詢定時(shí)器的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
python基于event實(shí)現(xiàn)線程間通信控制
這篇文章主要介紹了python基于event實(shí)現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
python多進(jìn)程執(zhí)行方法apply_async使用說明
這篇文章主要介紹了python多進(jìn)程執(zhí)行方法apply_async使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python爬取百度翻譯實(shí)現(xiàn)中英互譯功能
這篇文章主要介紹了利用Python爬蟲爬取百度翻譯,從而實(shí)現(xiàn)中英文互譯的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-01-01
Python 實(shí)現(xiàn)Excel XLS和XLSX格式相互轉(zhuǎn)換問題
本文介紹如何使用Python庫Spire.XLS for Python實(shí)現(xiàn)Excel文件的XLS和XLSX格式轉(zhuǎn)換,提供了詳細(xì)的安裝指南和轉(zhuǎn)換步驟,幫助用戶在不同版本的Excel文件格式之間靈活轉(zhuǎn)換,同時(shí)支持將Excel文件轉(zhuǎn)換為PDF、圖片、HTML等格式2024-10-10
控制Python浮點(diǎn)數(shù)輸出位數(shù)的操作方法
在python的輸出結(jié)果中,尤其是浮點(diǎn)數(shù)的輸出,當(dāng)我們需要寫入文本文件時(shí),最好是采用統(tǒng)一的輸出格式,這樣也能夠增強(qiáng)結(jié)果的可讀性,這篇文章主要介紹了控制Python浮點(diǎn)數(shù)輸出位數(shù)的方法,需要的朋友可以參考下2022-04-04

