主頁(yè) > 知識(shí)庫(kù) > python內(nèi)置函數(shù)之slice案例詳解

python內(nèi)置函數(shù)之slice案例詳解

熱門標(biāo)簽:美圖手機(jī) 銀行業(yè)務(wù) 呼叫中心市場(chǎng)需求 網(wǎng)站文章發(fā)布 檢查注冊(cè)表項(xiàng) 鐵路電話系統(tǒng) 智能手機(jī) 服務(wù)器配置

英文文檔:

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.

說(shuō)明:

  1. 函數(shù)實(shí)際上是一個(gè)切片類的構(gòu)造函數(shù),返回一個(gè)切片對(duì)象。

  2. 切片對(duì)象由3個(gè)屬性start、stop、step組成,start和step默認(rèn)值為None。切片對(duì)象主要用于對(duì)序列對(duì)象進(jìn)行切片取對(duì)應(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. 對(duì)應(yīng)切片對(duì)象的3個(gè)屬性start、stop、step,slice函數(shù)也有3個(gè)對(duì)應(yīng)的參數(shù)start、stop、step,其值分別會(huì)付給切片對(duì)象的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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python:slice與indices的用法
  • Python高級(jí)特性切片(Slice)操作詳解
  • python中slice參數(shù)過(guò)長(zhǎng)的處理方法及實(shí)例
  • 詳解python編程slice與indices函數(shù)用法示例

標(biāo)簽:沈陽(yáng) 河南 上海 滄州 紅河 新疆 樂(lè)山 長(zhǎng)治

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python內(nèi)置函數(shù)之slice案例詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266