当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Come and learn! Development Guide for personalized recommendation system (with internet disk link)
- 价值超10亿美元的直播系统架构图是什么样子的?
- Dongge ate grapes when he ate an algorithm problem!
- SSL证书和公网IP地址如何影响SEO
- ES6, ES7, es8 Learning Guide
- Common settings of PLSQL developer
- Functional guide for temporary users and novices of PL / SQL developer
- 公网IP地址和SSL证书可以提升SEO吗?
- SQL case conversion, remove the space before and after
- [graffiti Internet of things footprint] graffiti cloud platform interface description
猜你喜欢

SQL server attached database access denial resolution summary

CUDA_存储器模型

东哥吃葡萄时竟然吃出一道算法题!

The problem of looting by leetcode

Quick for imx6ull development board c program call shell

那么当下的行情投资者该怎么办呢? 现在新的投资风口来了!

技术点5:XML语言

How to carry out modular power operation efficiently

CUDA_共享内存、访存机制、访问优化

SQL intercepts the data before and after the '.'
随机推荐
IP地址SSL证书
Hand in hand to teach you to use container service tke cluster audit troubleshooting
CUDA_全局内存及访问优化
必看!RDS 数据库入门一本通(附网盘链接)
Error running app:Default Activity not found 解决方法
Performance evaluation report of YoMo codec - Y3
asp.net core中使用Serilog以及自定义Enricher
How can financial management system help enterprises realize financial automation management?
JT Jingtao project
表单验证,为避免全局污染,少定义全局变量写法
在PHP7下怎么大幅度提升Laravel框架性能?安装Stone!
如何k个一组反转链表
Operation! Nested JSON second change dataframe!
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
The movie theater booking system based on micro Service Framework
Daily page number style problems
The problem of looting by leetcode
PLSQL Developer常用设置
crm系统的成本一般是多少?
Prometheus安装配置