iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python切片索引越界的问题(数组下标越界)
  • 188
分享到

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

2024-04-02 19:04:59 188人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

前言 python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,比如:LeetCode_5。 学习官方解法时发现切片的索引可以超出字符串或数组最大索引值,此时编译器不会

前言

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 切片索引越界内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

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

本文链接: https://www.lsjlt.com/news/160450.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python切片索引越界的问题(数组下标越界)
    前言 Python语言处理字符串、数组类的问题时有一定概率需要使用切片方法,比如:Leetcode_5。 学习官方解法时发现切片的索引可以超出字符串或数组最大索引值,此时编译器不会...
    99+
    2022-11-12
  • Python切片会索引越界吗
    本篇内容主要讲解“Python切片会索引越界吗”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python切片会索引越界吗”吧!切片主要用于序列对象中,按照索引区间截取出一段索引的内容。切片的书写...
    99+
    2023-06-22
  • Python脚本索引越界的问题
    在Python中,脚本索引越界的问题通常会导致IndexError异常。索引越界是指尝试访问列表、元组、字符串或其他可迭代对象中不存...
    99+
    2023-08-18
    Python
  • Python 切片为什么不会索引越界?
    切片主要用于序列对象中,按照索引区间截取出一段索引的内容。 切片的书写形式:[i : i+n : m] ;其中,i 是切片的起始索引值,为列表首位时可省略;i+n 是切片的结束位置,...
    99+
    2022-11-12
  • Python脚本索引越界问题怎么解决
    Python脚本索引越界问题可以通过以下几种方式解决:1. 检查索引范围:在访问索引之前,先检查索引是否越界。可以使用条件语句(例如...
    99+
    2023-08-18
    Python
  • C语言数组越界引发的死循环问题解决
    目录一、引入二、代码缺陷三、为什么会死循环?四、补充说明五、总结一、引入 下面的程序在VS编译器会出现什么问题?运行结果是什么?为什么? #include <stdio.h&g...
    99+
    2022-11-13
    C语言 数组越界
  • C++中常见的数组越界问题解决方案
    C++中常见的数组越界问题解决方案,需要具体代码示例在C++编程中,数组越界是一个常见的错误。当我们在访问数组中的元素时超出了数组的索引范围,就会导致程序出现未定义的行为。为了避免这类错误,我们需要采取一些解决方案。解决方案一:正确使用数组...
    99+
    2023-10-22
    异常处理 (Exception Handling) 边界检查 (Boundary checking) 调试工具 (Deb
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作