Python 切片索引越界的问题(数组下标越界)

 更新时间:2021年12月21日 10:53:30   作者:KoenigseggH  
Python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,本文主要介绍了Python 切片索引越界的问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

前言

Python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,比如:Leetcode_5。
学习官方解法时发现切片的索引可以超出字符串或数组最大索引值,此时编译器不会报错。
欢迎大佬留言说明这种情况的具体原因,本文只进行一些情况的简单测试。

实例代码

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

发现结果为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 切片索引越界内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 从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编程的园地,才发现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开发一个功能全面的Markdown编辑工具

    利用Python开发一个功能全面的Markdown编辑工具

    这篇文章主要为大家详细介绍了如何利用Python开发一个功能全面的Markdown编辑工具,支持Markdown内容的编辑,HTML预览等功能,需要的可以参考下
    2025-03-03
  • pandas groupby + unstack的使用说明

    pandas groupby + unstack的使用说明

    这篇文章主要介绍了pandas groupby + unstack的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python并发编程之进程间通信原理及实现解析

    Python并发编程之进程间通信原理及实现解析

    这篇文章主要为大家介绍了Python并发编程之进程间通信原理及实现解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01

最新评论