Python 切片索引越界的問(wèn)題(數(shù)組下標(biāo)越界)
前言
Python語(yǔ)言處理字符串、數(shù)組類(lèi)的問(wèn)題時(shí)有一定概率需要使用切片方法,比如:Leetcode_5。
學(xué)習(xí)官方解法時(shí)發(fā)現(xiàn)切片的索引可以超出字符串或數(shù)組最大索引值,此時(shí)編譯器不會(huì)報(bào)錯(cuò)。
歡迎大佬留言說(shuō)明這種情況的具體原因,本文只進(jìn)行一些情況的簡(jiǎn)單測(cè)試。
實(shí)例代碼
a = '123' b = a[:5] print(b)
發(fā)現(xiàn)結(jié)果為123,編譯器沒(méi)有報(bào)錯(cuò)。而當(dāng)直接使用a[5]時(shí)即報(bào)錯(cuò)string index out of range。下面是測(cè)試結(jié)果。
測(cè)試代碼(字符串)
a = "1234567890" a1 = a[:] a2 = a[:len(a)] a3 = a[:15] a4 = a[16:16] a5 = a[:2]
運(yùn)行結(jié)果:
This is the id of 'a' :? 2707772994160
This is the type of 'a' :? <class 'str'>
This is the value of 'a' :? 1234567890This is the id of 'a1' :? 2707772994160
This is the type of 'a1' :? <class 'str'>
This is the value of 'a1' :? 1234567890This is the id of 'a2' :? 2707772994160
This is the type of 'a2' :? <class 'str'>
This is the value of 'a2' :? 1234567890This is the id of 'a3' :? 2707772994160
This is the type of 'a3' :? <class 'str'>
This is the value of 'a3' :? 1234567890This is the id of 'a4' :? 2707740774832
This is the type of 'a4' :? <class 'str'>
This is the value of 'a4' :?This is the id of 'a5' :? 2707773122544
This is the type of 'a5' :? <class 'str'>
This is the value of 'a5' :? 12
值得注意的地方:
- 若切片后結(jié)果與原來(lái)相同,則新字符串所指向的物理地址就是原字符串的物理地址(a1、a2、a3)。
- 若切片后結(jié)果與原來(lái)不同,則新字符串指向新的物理地址(a5)。
- 若當(dāng)前切片索引范圍內(nèi)不存在合法數(shù)值,則返回相應(yīng)類(lèi)型的空值(a4)。
測(cè)試代碼(數(shù)組)
b = [1, 2, 3, 4, 5] b1 = b[:] b2 = b[:len(b)] b3 = b[:15] b4 = b[16:16] b5 = b[:2]
This is the id of 'b' :? 2260784433096
This is the type of 'b' :? <class 'list'>
This is the value of 'b' :? [1, 2, 3, 4, 5]This is the id of 'b1' :? 2260784432456
This is the type of 'b1' :? <class 'list'>
This is the value of 'b1' :? [1, 2, 3, 4, 5]This is the id of 'b2' :? 2260784470920
This is the type of 'b2' :? <class 'list'>
This is the value of 'b2' :? [1, 2, 3, 4, 5]This is the id of 'b3' :? 2260784534280
This is the type of 'b3' :? <class 'list'>
This is the value of 'b3' :? [1, 2, 3, 4, 5]This is the id of 'b4' :? 2260784471432
This is the type of 'b4' :? <class 'list'>
This is the value of 'b4' :? []This is the id of 'b5' :? 2260784231944
This is the type of 'b5' :? <class 'list'>
This is the value of 'b5' :? [1, 2]
值得注意的地方:
- 數(shù)組切片操作必定指向新的物理地址。
- 若當(dāng)前切片索引范圍內(nèi)不存在合法數(shù)值,則返回相應(yīng)類(lèi)型的空值(b4)。
到此這篇關(guān)于Python 切片索引越界的實(shí)現(xiàn)(數(shù)組下標(biāo)越界)的文章就介紹到這了,更多相關(guān)Python 切片索引越界內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python分段函數(shù)的實(shí)現(xiàn)示例
分段函數(shù)是一種數(shù)學(xué)函數(shù),它將定義域分成若干個(gè)區(qū)間,每個(gè)區(qū)間對(duì)應(yīng)一個(gè)函數(shù),本文主要介紹了python分段函數(shù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12python實(shí)現(xiàn)梯度法 python最速下降法
這篇文章主要為大家詳細(xì)介紹了python梯度法,最速下降法的原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Python3操作讀寫(xiě)CSV文件使用包過(guò)程解析
這篇文章主要介紹了Python3操作CSV文件使用包過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python 實(shí)戰(zhàn)開(kāi)發(fā)校園管理系統(tǒng)詳細(xì)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開(kāi)發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-10-10對(duì)python同一個(gè)文件夾里面不同.py文件的交叉引用方法詳解
今天小編就為大家分享一篇對(duì)python同一個(gè)文件夾里面不同.py文件的交叉引用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹及功能實(shí)現(xiàn)
backward()?函數(shù)是PyTorch框架中自動(dòng)求梯度功能的一部分,它負(fù)責(zé)執(zhí)行反向傳播算法以計(jì)算模型參數(shù)的梯度,這篇文章主要介紹了PyTorch中tensor.backward()函數(shù)的詳細(xì)介紹,需要的朋友可以參考下2024-02-02基于Python實(shí)現(xiàn)n-gram文本生成的示例代碼
N-gram是自然語(yǔ)言處理中常用的技術(shù),它可以用于文本生成、語(yǔ)言模型訓(xùn)練等任務(wù),本文主要介紹了如何在Python中實(shí)現(xiàn)n-gram文本生成,需要的可以參考下2024-01-01