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


猜你喜欢
- OpenCV介绍OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、Android和Ma
- 一、MySQL备份类型详解对于现代互联网公司而言,存储在服务器数据库中的数据,逐步成为企业和公司的命脉,对企业和公司的生存发展具有十分重大的
- 把程序放到一个文件中,然后包含再call就可以了。(JMAIL4.3)<%'警告函数sub w_msg(messag
- 1.查看binlog是否开启show variables like '%log_bin%';2.查看数据文件存放路径:bin
- 在自动化测试过程中,有时后会遇到元素定位方式没有问题,但是依旧抛出无法找到元素的异常的问题,通常情况下,如果元素定位没有问题,但还是无法找到
- 示例matplotlib中的animation提供了动态绘图功能,下面列举一个最简单的动态绘制三角函数的例子,来初步演示一下。import
- 1. 需要用到的Python库pygametkinter2. 简易UI设计audio_player = Tk()audio_player.t
- 概念django自带一套信号机制来帮助我们在框架的不同位置之间传递信息。也就是说,当某一事件发生时,信号系统可以允许一个或多个发送者(sen
- 最终效果前言这是最近在学qt这个东西,然后又学会了调用api,然后就想了用pyqt5做一个GUI界面,后期也可以打包分享给其他人使用,所以就
- 1、方法说明import cv2 as cvimg1 =cv.imread(filename[, flags])参数说明filename图片
- 一、基本使用①从属于time这个包②一般使用都是使用time.Time 这个类型表示时间 ,time包中还有一些常量,源码如下// Comm
- //如果之前有选中的,则把选中radio取消掉$("#tj_cat .pro_category").each(funct
- Python 解释器内置了一些常量和函数,叫做内置常量(Built-in Constants)和内置函数(Built-in Function
- Django 是由 Python 开发的一个免费的开源网站框架,可以用于快速搭建高性能,优雅的网站!学习django学得超级吃力
- (一) 常用的CSS命名规则:头:header内容:content/container尾:footer导航:nav侧栏:sidebar栏目:
- PM2实现Nodejs项目自动部署首先简单说下思路:本地git仓库与远程仓库关联(github、码云等平台),然后pm2按照指定配置登录服务
- 插入一条记录后,如何得到最新的自动增加ID?我们要用到SQL Server的@@IDENTITY。它能够记录下系统最近使用的一个IDENTI
- 1.什么是Blazor? 有什么优势?ASP.NET Core Blazor 简介Blazor 是一个使用 Blazor 生成交互式客户端
- 关于ref和$refs的用法及讲解,vue.js中文社区( https://cn.vuejs.org/v2/api/#ref )是这么讲解的
- replace() 方法用于将字符串用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。 需要注意的是:如果用正则表达式替换时, r