当前位置:网站首页>Usage of [:] and [::] in Python
Usage of [:] and [::] in Python
2020-11-09 22:37:00 【Giant ship】
Python in [:] And [::] Usage of
summary
[:] And [::] Syntax is a general sequential operation (Common Sequence Operations) Two of them . use [:] or [::] For most sequence types ( Variable or immutable )( Such as a string 、 List etc. ) The elements in the sequence are intercepted .
[:] Usage of
s[i:j]
From the subscript i To the subscript j, Intercept sequence s The elements in .
- If i or j It's a negative number , Then the subscript starts at the end of the sequence , in other words , Subscript i It's equivalent to a subscript len(s)+i, Subscript j It's equivalent to a subscript len(s)+j. in addition ,-0 Is still 0.
- From sequence s The subscript i The elements of ( contain ) To the subscript j The elements of ( It doesn't contain ) Between all the elements , Intercept . in addition ,i < j.
- if i or j Greater than len(s), be i or j By len(s) replace .
- if i Omit or omit i yes None value , be i By 0 replace .
- if j Omit or omit j yes None value , be j By len(s) replace .
- if i>=j, Returns a sequence without elements .
>>> 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]
[]
>>>
[::] Usage of
s[i:j:k]
From the subscript i To the subscript j, The steps are k, Intercept sequence s The elements in .
- If i or j It's a negative number , Then the subscript starts at the end of the sequence , in other words , Subscript i It's equivalent to a subscript len(s)+i, Subscript j It's equivalent to a subscript len(s)+j. in addition ,-0 Is still 0.
- From sequence s The subscript i The elements of ( contain ) To the subscript j The elements of ( It doesn't contain ) Between , Select the subscript as i+n*k The elements of (0 <= n < (j-i)/k), in other words , Select the subscript as i, i+k, i +2*k, i+3*k And so on , Until the subscript is greater than or equal to j until .
- When k Is a positive number , Subscript selection reverses from 0 To len(s)-1.
- When k It's a negative number , Subscript selection reverses from len(s)-1 To 0.
- When i or j Omit or omit None value , Then the end subscript (0 or len(s)-1) replaced ( To be specific, see k The positive and negative signs of ).
- k Not for 0. if k by None value , Will k as 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]
>>>
Reference material
版权声明
本文为[Giant ship]所创,转载请带上原文链接,感谢
边栏推荐
- DB engines database ranking in November: PostgreSQL holds the top spot in the same period
- Expect ':' at 0, actual = (JSON conversion exception resolution)
- Operation and design of rights management in ERP
- Application of V7 version of lvgl Library
- 算法模板整理(一)
- Realization of commodity backstage system
- Python提示AttributeError 或者DeprecationWarning: This module was deprecated解决方法
- How to greatly improve the performance of larravel framework under php7? Install stone!
- C + + game development
- Important components of Apache Hadoop
猜你喜欢

代码中的软件工程--对menu项目的源码分析

LinkedList源码简析

Application of V7 version of lvgl Library

How to upload your avatar with code and add your own copyright information?

IP address SSL certificate

Hand in hand to teach you to use container service tke cluster audit troubleshooting

day84:luffy:优惠活动策略&用户认证&购物车商品的勾选/结算

Make a home page

Common settings of PLSQL developer

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
随机推荐
Quick for imx6ull development board c program call shell
From master of Engineering Physics of Tsinghua University to open source entrepreneur of cloud computing
Software engineering in code -- source code analysis of menu project
On the practical application of C 9's new features
SRM系统是什么系统?SRM供应商管理系统功能
Performance evaluation report of YoMo codec - Y3
Kubernetes-18: installation and use of dashboard
The number of more than half of the array is printed by the sword
快来学习!个性化推荐系统开发指南(附网盘链接)
Baishan cloud technology is selected as the top 100 Internet enterprises in China in 2020
SQL case conversion, remove the space before and after
Markdown plug-in of vscode
商品后台系统实现
如何实现LRU算法
win7+vs2015+cuda10.2配置TensorRT7.0
JS deep copy
PHP - curl copy paste access SMS verification code example
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Configure the NZ date picker time selection component of ng zerro
Make a home page