最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 切片索引越界的問題(數組下標越界)

 更新時間:2021年12月21日 10:53:30   作者:KoenigseggH  
Python語言處理字符串、數組類的問題時有一定概率需要使用切片方法,本文主要介紹了Python 切片索引越界的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

Python語言處理字符串、數組類的問題時有一定概率需要使用切片方法,比如:Leetcode_5。
學習官方解法時發(fā)現切片的索引可以超出字符串或數組最大索引值,此時編譯器不會報錯。
歡迎大佬留言說明這種情況的具體原因,本文只進行一些情況的簡單測試。

實例代碼

a = '123'
b = a[:5]
print(b)

發(fā)現結果為123,編譯器沒有報錯。而當直接使用a[5]時即報錯string index out of range。下面是測試結果。

測試代碼(字符串)

a = "1234567890"
a1 = a[:]
a2 = a[:len(a)]
a3 = a[:15]
a4 = a[16:16]
a5 = a[:2]

運行結果:

This is the id of 'a' :? 2707772994160
This is the type of 'a' :? <class 'str'>
This is the value of 'a' :? 1234567890

This is the id of 'a1' :? 2707772994160
This is the type of 'a1' :? <class 'str'>
This is the value of 'a1' :? 1234567890

This is the id of 'a2' :? 2707772994160
This is the type of 'a2' :? <class 'str'>
This is the value of 'a2' :? 1234567890

This is the id of 'a3' :? 2707772994160
This is the type of 'a3' :? <class 'str'>
This is the value of 'a3' :? 1234567890

This 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

值得注意的地方:

  • 若切片后結果與原來相同,則新字符串所指向的物理地址就是原字符串的物理地址(a1、a2、a3)。
  • 若切片后結果與原來不同,則新字符串指向新的物理地址(a5)。
  • 若當前切片索引范圍內不存在合法數值,則返回相應類型的空值(a4)。

測試代碼(數組)

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]

值得注意的地方:

  • 數組切片操作必定指向新的物理地址。
  • 若當前切片索引范圍內不存在合法數值,則返回相應類型的空值(b4)。

到此這篇關于Python 切片索引越界的實現(數組下標越界)的文章就介紹到這了,更多相關Python 切片索引越界內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 從DataFrame中提取出Series或DataFrame對象的方法

    從DataFrame中提取出Series或DataFrame對象的方法

    今天小編就為大家分享一篇從DataFrame中提取出Series或DataFrame對象的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python fabric實現遠程操作和部署示例

    python fabric實現遠程操作和部署示例

    這篇文章主要介紹了python使用fabric實現遠程操作和部署示例,需要的朋友可以參考下
    2014-03-03
  • Python去除html標簽的幾種方法總結

    Python去除html標簽的幾種方法總結

    這篇文章主要介紹了Python去除html標簽的幾種方法總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python基礎學習之奇異的GUI對話框

    Python基礎學習之奇異的GUI對話框

    今天跨進了GUI編程的園地,才發(fā)現python語言是這么的好玩,文中對GUI對話框作了非常詳細的介紹,對正在學習python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • 深入了解Python中的時間處理函數

    深入了解Python中的時間處理函數

    這篇文章主要是和大家一起探索python中的時間處理函數,讓大家徹底弄懂時間處理。文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2021-12-12
  • Python Numpy 實現交換兩行和兩列的方法

    Python Numpy 實現交換兩行和兩列的方法

    今天小編就為大家分享一篇Python Numpy 實現交換兩行和兩列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 淺談Python 函數式編程

    淺談Python 函數式編程

    這篇文章主要介紹了Python 函數式編程的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • 利用Python開發(fā)一個功能全面的Markdown編輯工具

    利用Python開發(fā)一個功能全面的Markdown編輯工具

    這篇文章主要為大家詳細介紹了如何利用Python開發(fā)一個功能全面的Markdown編輯工具,支持Markdown內容的編輯,HTML預覽等功能,需要的可以參考下
    2025-03-03
  • pandas groupby + unstack的使用說明

    pandas groupby + unstack的使用說明

    這篇文章主要介紹了pandas groupby + unstack的使用說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python并發(fā)編程之進程間通信原理及實現解析

    Python并發(fā)編程之進程間通信原理及實現解析

    這篇文章主要為大家介紹了Python并發(fā)編程之進程間通信原理及實現解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01

最新評論

土默特左旗| 伊宁县| 宝山区| 扎赉特旗| 沙洋县| 安宁市| 崇信县| 灵石县| 治多县| 平安县| 尉犁县| 汶川县| 南平市| 闻喜县| 曲阳县| 孟津县| 郯城县| 沁源县| 都匀市| 新田县| 东宁县| 普安县| 辉县市| 合水县| 高密市| 河津市| 勐海县| 周口市| 册亨县| 平定县| 全椒县| 察哈| 微山县| 大关县| 嫩江县| 南宁市| 乌苏市| 五寨县| 大安市| 无为县| 襄樊市|