Python学习--字符串slicing
2014-09-03 11:12
141 查看
Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages
In python 2.7Slicing in python[a:b:c]len = length of string, tuple or listc -- 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-1In backward direction, starts at -1 and ends at -lenwhen 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 -1but 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.
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
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.7Slicing in python[a:b:c]len = length of string, tuple or listc -- 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-1In backward direction, starts at -1 and ends at -lenwhen 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 -1but 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学习笔记七:字符串的操作(一)
- Python学习笔记之一:Python的字符串
- python学习笔记——字符串,列表,字典,集合,数值,sorted
- Python学习--识别“字符串&字典&列表&元组“
- python的学习(五)----字符串操作
- python学习笔记,整形,字符串
- Python学习笔记:字符串基本操作
- 【学习总结】python-DAY3-字符串
- python 学习记录(10)-字符串与正则表达式
- python2学习笔记 第三章 使用字符串
- Python学习(二) 字符串String
- python学习之 字符串前'r'的用法
- python学习笔记(二)字符串表示和运算
- python 3.2 字符串方法学习总结
- python学习笔记——字符串
- Python学习笔记八:字符串的操作(二)
- python学习笔记——字符串
- Python学习笔记整理(四)Python中的字符串..
- python字符串格式化 学习一
- python学习之 字符串前'r'的用法