当前位置:网站首页>Python中[:]与[::]的用法
Python中[:]与[::]的用法
2020-11-09 22:37:00 【巨輪】
Python中[:]与[::]的用法
概述
[:]与[::]语法是通用序列操作(Common Sequence Operations)其中的两个。用[:]或[::]对多数序列类型(可变的或不可变的)(如字符串、列表等)序列中元素进行截取。
[:]的用法
s[i:j]
从下标i到下标j,截取序列s中的元素。
- 如果i或j为负数,则下标从序列尾部算起,也就是说,下标i相当于下标len(s)+i,下标j相当于下标len(s)+j。另外,-0依然是0。
- 从序列s的下标i的元素(包含)到下标j的元素(不包含)之间所有元素,进行截取。另外,i < j。
- 若i或j大于len(s),则i或j被len(s)取代。
- 若i省略或i是None值,则i被0取代。
- 若j省略或j是None值,则j被len(s)取代。
- 若i>=j,返回没元素的序列。
>>> a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[-1:]
[9]
>>> a[-9:-1]
[1, 2, 3, 4, 5, 6, 7, 8]
>>> a[1:20]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:9]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> a[0:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[9:3]
[]
>>>
[::]的用法
s[i:j:k]
从下标i到下标j,步数为k,截取序列s中的元素。
- 如果i或j为负数,则下标从序列尾部算起,也就是说,下标i相当于下标len(s)+i,下标j相当于下标len(s)+j。另外,-0依然是0。
- 从序列s的下标i的元素(包含)到下标j的元素(不包含)之间,选取下标为i+n*k的元素(0 <= n < (j-i)/k),也就是说,选取下标为i, i+k, i +2*k, i+3*k等等的元素,直到下标大于等于j为止。
- 当k为正数,下标选取反向从0到len(s)-1。
- 当k为负数,下标选取反向从len(s)-1到0。
- 当i或j省略或是None值,则被端下标(0或len(s)-1)所取代(具体看k的正负符号)。
- k不能为0。若k为None值,则将k看成1。
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[1:10:2]
[1, 3, 5, 7, 9]
>>> a[1:10:3]
[1, 4, 7]
>>> a[1:10:-2]
[]
>>> a[1:10:-3]
[]
>>> a[10:1:-2]
[9, 7, 5, 3]
>>> a[10:1:-3]
[9, 6, 3]
>>> a[::2]
[0, 2, 4, 6, 8]
>>> a[::-2]
[9, 7, 5, 3, 1]
>>> a[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[-9:-1:2]
[1, 3, 5, 7]
>>> a[-9:-1:-2]
[]
>>> a[-1:-9:-2]
[9, 7, 5, 3]
>>> a[-1::-2]
[9, 7, 5, 3, 1]
>>> a[:-1:2]
[0, 2, 4, 6, 8]
>>>
参考资料
版权声明
本文为[巨輪]所创,转载请带上原文链接,感谢
https://my.oschina.net/jallenkwong/blog/4710314
边栏推荐
- Configure the NZ date picker time selection component of ng zerro
- Traditional purchasing mode has changed! How to innovate automobile purchasing function?
- Low power Bluetooth single chip helps Internet of things
- Kubernetes-18:Dashboard安装及使用
- How to greatly improve the performance of larravel framework under php7? Install stone!
- 如何实现LRU算法
- 动物园[CSP2020]
- 配置ng
- Performance evaluation report of YoMo codec - Y3
- JS deep copy
猜你喜欢
[best practice] learn how eolinker helps Telecommuting
Traditional purchasing mode has changed! How to innovate automobile purchasing function?
Git old bird search manual
都要2021年了,现代C++有什么值得我们学习的?
day84:luffy:优惠活动策略&用户认证&购物车商品的勾选/结算
Postman(一)---功能介绍
骚操作!嵌套 JSON 秒变 Dataframe!
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
AQS source code in-depth analysis of conditional queue
Apache Hadoop的重要组成
随机推荐
修改jar包里的文件
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
Realization of commodity backstage system
JT-day10
如何用代码上传头像,并添加自己的版权信息?
JT Jingtao project
Another comparison operator related interview question let me understand that the foundation is very important
Hot update scheme of Chrome extension program: 2. Based on double cache update function module
[最佳实践]了解 Eolinker 如何助力远程办公
SQL server attached database access denial resolution summary
【科创人】Rancher江鹏:从清华工程物理学硕士到云计算开源创业者
YoMo Codec - Y3的性能评测报告
Git old bird search manual
Performance evaluation report of YoMo codec - Y3
LeetCode 50 Pow(x,n)
Make a home page
It will be 2021. What is the modern C + + worth learning?
AQS source code in-depth analysis of conditional queue
LeetCode 49 字母异位词分组
白山云科技入选2020中国互联网企业百强