Python切片索引的使用方法是怎样,哪些事项要注意
Admin 2022-06-20 群英技术资讯 873 次浏览
这篇文章主要讲解了“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' : 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
值得注意的地方:
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]
值得注意的地方:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了Python测试框架pytest高阶用法全面详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要介绍了利用Python 实现图片转字符画,要将图片转字符画,需要先定义一个字符集,用来和灰度值做映射,将图片每个像素的 RGB 值转换为一个灰度值,将其对应的字符输出就得到字符画
这篇文章主要为大家介绍了pytest官方文档解读fixtures的autouse,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
Vim 插件是一个 .vim 的脚本文件,定义了函数、映射、语法规则和命令,可用于操作窗口、缓冲以及行。一般一个插件包含了命令定义和事件钩子。当使用 Python 编写 vim 插件时,函数外面是使用 VimL 编写,尽管 VimL 学起来很快,但 Python 更加灵活
内容介绍题目描述解题思路/算法分析/问题及解决实验代码运行结果题目描述本次实验为连接数据库的实验,并对数据库进行一些简单的操作,要实现的基本功能如下所示,要能连接并展现数据库里的数据,能够实现插入功能
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008