Python学习--字符串slicing
Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages
Python indexes and slices for a six-element list.
Indexes enumerate the elements, slices enumerate the spaces between the elements.
Index from rear: -6 -5 -4 -3 -2 -1 a=[0,1,2,3,4,5] a[1:]==[1,2,3,4,5]
Index from front: 0 1 2 3 4 5 len(a)==6 a[:5]==[0,1,2,3,4]
+---+---+---+---+---+---+ a[0]==0 a[:-2]==[0,1,2,3]
| a | b | c | d | e | f | a[5]==5 a[1:2]==[1]
+---+---+---+---+---+---+ a[-1]==5 a[1:-1]==[1,2,3,4]
Slice from front: : 1 2 3 4 5 : a[-2]==4
Slice from rear: : -5 -4 -3 -2 -1 :
b=a[:]
b==[0,1,2,3,4,5] (shallow copy of a)
In python 2.7
Slicing in python
[a:b:c]
len = length of string, tuple or list
c -- default is +1. sign of c indicates forward or backward, absolute value of c indicates steps. Default is forward with step size 1. Positive means forward, negative means backward.
a -- when c is positive or blank, default is 0. when c is negative, default is -1.
b -- when c is positive or blank, default is len. when c is negative, default is -(len+1).
Understanding index assignment is very important.
In forward direction, starts at 0 and ends at len-1
In backward direction, starts at -1 and ends at -len
when you say [a:b:c] you are saying depending on sign of c (forward or backward), start at a and end at b ( excluding element at bth index). Use the indexing rule above and remember you will only find elements in this range
-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1
but this range continues in both directions infinitely
...,-len -2 ,-len-1,-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1, len, len +1, len+2 , ....
e.g.
0 1 2 3 4 5 6 7 8 9 10 11
a s t r i n g
-9 -8 -7 -6 -5 -4 -3 -2 -1
if your choice of a , b and c allows overlap with the range above as you traverse using rules for a,b,c above you will either get a list with elements (touched during traversal) or you will get an empty list.
One last thing: if a and b are equal , then also you get an empty list
- >>> l1
- [2, 3, 4]
- >>> l1[:]
- [2, 3, 4]
- >>> l1[::-1] # a default is -1 , b default is -(len+1)
- [4, 3, 2]
- >>> l1[:-4:-1] # a default is -1
- [4, 3, 2]
- >>> l1[:-3:-1] # a default is -1
- [4, 3]
- >>> l1[::] # c default is +1, so a default is 0, b default is len
- [2, 3, 4]
- >>> l1[::-1] # c is -1 , so a default is -1 and b default is -(len+1)
- [4, 3, 2]
- >>> l1[-100:-200:-1] # interesting
- []
- >>> l1[-1:-200:-1] # interesting
- [4, 3, 2]
- >>> l1[-1:5:1]
- [4]
- >>> l1[-1:-1:1]
- []
- >>> l1[-1:5:1] # interesting
- [4]
- >>> l1[1:-7:1]
- []
- >>> l1[1:-7:-1] # interesting
- [3, 2]
Python学习--字符串slicing的更多相关文章
- python学习--字符串
python的字符串类型为str 定义字符串可以用 ‘abc' , "abc", '''abc''' 查看str的帮助 在python提示符里 help(str) python基于 ...
- python学习-字符串 列表 元祖
目录 Python翻转字符串(reverse string) 简单的步长为-1, 即字符串的翻转(常用) 递归反转 借用列表,使用reverse()方法 字符串常用操作 index split 切片 ...
- python学习——字符串
1)字符串解释 字符串是python中常用的数据类型我们可以使用" "或' '来创建字符串. 2)字符串操作 """访问字符串中的值"&qu ...
- python学习-字符串前面添加u,r,b的含义
引用:https://www.cnblogs.com/cq90/p/6959567.html u/U:表示unicode字符串 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unico ...
- Python学习---字符串处理
This world is but a canvas to our imagination. 世界只是我们的想象的画布. ----Apri 22 ''' 题目内容: "Pig Latin&q ...
- Python学习---字符串操作
### 截取字符串然后拼接 str = "Hello World!" str2 = str[:6] + "tyche !" print(str2) ===> ...
- Python学习-字符串的基本知识
字符串的基本知识 根据所展示形式的不同,字符串也可以分为两类 原始字符串: 使用单引号包括:‘liuwen’ 使用双引号包括:"liuwen" 使用3个单引号包括 :'''liuw ...
- Python学习-字符串函数操作3
字符串函数操作 isprintable():判断一个字符串中所有字符是否都是可打印字符的. 与isspace()函数很相似 如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 ...
- Python学习-字符串函数操作2
字符串函数操作 find( sub, start=None, end=None):从左到右开始查找目标子序列,找到了结束查找返回下标值,没找到返回 -1 sub:需要查找的字符串 start=None ...
随机推荐
- 常用包管理三类工具:dpkg、apt和aptitude
常用的包管理包含三类工具:dpkg.apt和aptitude.人们总是对前面的两个工具用得比较多,而对 aptitude 用得比较少,事实上 aptitude 是很强大的. 在这里,对这三个工具做一点 ...
- 技能UP:SAP CO掌上配置手册
No. 配置对象 事务代码 路径 1 Enterprise Structure and General Controlling configration Maintain EC-PCA : ...
- 对 /etc/rc.d/init.d 目录的一点理解
转载 一.Linux的引导过程 系统启动之后,在进入init.d之前,我们先来看看系统都做了什么工作.系统加电之后,首先进行的硬件自检,然后是bootload对系统的初始化,加载内核. 内核被加载到内 ...
- CI框架 -- 核心文件 之 Benchmark.php
Benchmark.php文件中定义的CI_Benchmark类可以让你标记点,并计算它们之间的时间差.还可以显示内存消耗. Benchmarking类库,它是被系统自动被加载的,不需要手工加载 cl ...
- php -- php模拟浏览器访问网址
目前我所了解到的在php后台中,用php模拟浏览器访问网址的方法有两种: 第一种:模拟GET请求:file_get_contents($url) 通过php内置的 file_get_contents ...
- Aspose.Words对于Word的操作
对于word操作一般是对已有word模板的操作,直接新建的不考虑,网上教程很多,自己看吧一般有以下几种办法(忘了具体几种了,一般情况下以下就够了)1.通过书签替换顾名思义,就是先定义一个书签,然后在书 ...
- Oracle备份与恢复介绍(物理备份与逻辑备份) 分类: Oracle 2015-07-27 22:59 15人阅读 评论(0) 收藏
算是挺全的了,有命令有真相 原文链接:http://blog.chinaunix.net/uid-354915-id-3525989.html 一.Oracle备份方式分类: Oracle有两类备份方 ...
- php中对象(object)与数组(array)之间的相互转换
/** * 数组 转 对象 * * @param array $arr 数组 * @return object */ function array_to_object($arr) { if (gett ...
- linux 下启动tomca慢问题
编辑文件vim /etc/profile 后面加入一句:export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom" 设置立 ...
- OCA,OCP,OCM傻傻分不清?
可能大家知道OCA.OCP.OCM的关系是一个比一个难考,一个比一个含金量高,但是你知道具体的考试科目.考试方式.就业形势区别吗?不知道的话这篇通俗易懂的文章会让你一目了然. 区别一:含金量 ■OCA ...