【说站】python切片符号的使用
Python 使用 符号 切片
2023-06-13 09:13:21 时间
python切片符号的使用
a[start:stop] # items start through stop-1
a[start:] # items start through the rest of the array
a[:stop] # items from the beginning through stop-1
a[:] # a copy of the whole array
还有一个step值,可以与上述任何一个一起使用:
a[start:stop:step] # start through not past stop, by step
要记住的关键点是该:
1、stop值表示不在所选切片中的第一个值。之间的差stop和start是选择的元素的数量(如果step是1,默认值)。
2、startorstop可能是一个负数,这意味着它从数组的末尾而不是开头开始计数。
所以:
a[-1] # last item in the array
a[-2:] # last two items in the array
a[:-2] # everything except the last two items
同样,step可能是负数:
a[::-1] # all items in the array, reversed
a[1::-1] # the first two items, reversed
a[:-3:-1] # the last two items, reversed
a[-3::-1] # everything except the last two items, reversed
如果项目少于您的要求,Python 对程序员是友好的。例如,如果你请求a[:-2]并且a只包含一个元素,你会得到一个空列表而不是错误。有时您更喜欢错误,因此您必须意识到这可能会发生。
以上就是python切片符号的使用,希望对大家有所帮助。
相关文章
- 使用 setup.py 将 Python 库打包分发到 PyPI 踩坑指南
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- 使用python的pyecharts库绘制数据可视化大屏
- 多重共线性:python计算VIF以及使用vif做因子独立性检验的方法「建议收藏」
- python hashlib_Python hashlib模块实例使用详解
- python海龟作图红绿灯_海龟作图—用Python绘图
- python进程池的使用
- 在python中用来安装第三方库的常用工具_什么库用于安装管理Python扩展包
- 使用Python批量给图片添加文字
- 基于Unittest框架,使用Python+Selenium+Webdriver的WebUI自动化测试项目应用实例(附源码)
- 【Python】PyCharm 基本使用 ① ( PyCharm 中创建 Python 工程 | 创建工程 | 设置工程路径 | 设置工程 Python 解释器 )
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与SQLite数据库-使用Python执行SQLite查询(二)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(二)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(三)
- python-Python与PostgreSQL数据库-使用Python执行PostgreSQL查询(一)
- python-Django 高级特性-Django 分页(一)
- python 多线程优先队列Queue详解编程语言
- Python开发系列课程(7) – 函数和模块的使用详解编程语言
- 使用Python操作MySQL数据库快速上手(python访问mysql数据库)
- 使用Python连接SQL Server数据库(python连接sqlserver数据库)
- Python玩转Redis:提升缓存效率(python使用redis)
- 使用Python连接SQL Server数据库(python连接sqlserver)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- Python正则表达式的七个使用范例详解