Python 切片索引越界的问题(数组下标越界)
作者:KoenigseggH 发布时间:2023-09-11 23:00:30
前言
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' : 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
值得注意的地方:
若切片后结果与原来相同,则新字符串所指向的物理地址就是原字符串的物理地址(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)。
来源:https://blog.csdn.net/jh_210/article/details/107768652
猜你喜欢
- 前几天,为了增强本站的SEO,着手把另一个域名:www.aspxhome.com下的所有页面301转向到www.cidianwang.com
- 环境:python3 + unittest + requestsExcel管理测试用例,HTMLTestRunner生成测试报告测试完成后邮
- Go语言集成开发环境之VS Code安装使用VS Code是微软开源的一款编辑器,插件系统十分的丰富。下面介绍如何用VS Code搭建go语
- SQL Server有两种备份方式,一种是使用BACKUP DATABASE将数据库文件备份出去,另外一种就是直接拷贝数据库文件mdf和日志
- bytes 和 bytearraybytes 和 bytearray 都是二进制世界的成员,用二进制的方式去理解才能看清他的本质。理解byt
- PDOStatement::bindParamPDOStatement::bindParam — 绑定一个参数到指定的变量名(PHP 5 &
- 最近项目很忙没机会更新博客。有朋友在和我谈学艺术的就业问题,就随便谈一下自己的想法。每年中国美院报考的人数与日俱增,越来越多的人投入到艺术设
- 从本篇开始讲述如何用css实现网页的布局,即如何用css控制网页内各个元素的显示位置。如果你是一个初学者,很可能觉得做一个网页的第一步就是布
- 设计网站的同志背景主要有两种:学计算机、学艺术。基本上会写代码的不懂设计,会设计的不懂代码,这个格局似乎到今天还没变。某些学计算机的同学,有
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 21 - Classes part
- 中间那个控制块,其实也是一个iframe,把他的宽度定义为10。 然后在他的内部js,控制 左右2个iframe. functio
- Server对象提供对服务器上访问的方法和属性.大多数方法和属性是作为实用程序的功能提供的。语法:Server.property|metho
- 今天有业务需要制作用户头像的需求,在网上找了个可以裁剪大图制作自己希望大小的图片的方法(基于Struts2)。特此记录一下。不废话,具体的步
- 数据库计算机 databasecomputer 实现数据库的存储、管理和控制的一种专用计算机系统。它能十分快速而有效地完成各种数据库操作,并
- 有些时候(如开发聊天程序),我们需要将将滚动条(scrollbar)保持在最底部,比如聊天窗口,最新发出和收到的信息要显示在最下方,如果要看
- 我们可以利用err对象来判断。当程序没有出现错误就说明已经执行了sql操作: sql="insert into
- 本文介绍了在js和asp中使用FileSystemObject(fso)来: 创建、添加或删除数据,以及读取文件; 移动、复制和删除文件;创
- 1、查找表结构,判断要加入的列是否已存在2、如果不存在,则执行添加 CREATE PROCEDURE `mysql_sp_add_
- 写个小教程,以期能让大家对jQuery有所了解,甚至喜欢上它(请不要怪我)。不废话,先说明我们的目的。我们知道,当代浏览器(modern b
- 基本原理使用Adodb.Stream读二进制文件然后进行解析,然后返回一数组第一个元素为类型(BMP JPG PNG GIF SWF)第二个