首先:說明什么是序列?
序列中的每一個元素都會被分配一個序號,即元素的位置,也稱為索引;在python中的序列包含:字符串、列表和元組
然后是:什么是分片?
分片就是通過操作索引訪問及獲得序列的一個或多個元素,也叫作切片。
如果序列中有N個元素,索引的范圍,正序是:0到N-1,逆序是:-N到-1
分片的操作一般需要三個參數,例如獲取a的第一個元素到第三個元素,則應該為:a[0,3,1]
其中0代表第一個元素的索引,3代表第三個元素的索引的后面的值,1代表遞增數,也就是從0開始每一次加1來獲取下一個元素,這個也可以通過for循環來實現
目前看到的對於分片來說,很明顯的一個例子就是:分片可以實現逆序,通過下方的代碼:
def f1():
list1 = [1,2,3,4,5]
list2 = list1[4::-1]
print list1
print list2
結果是:

遞增數為2的代碼如下:
def f2():
list1 = [1,2,3,4,5,6,7,8]
list2 = list1[2:7:2]
print list1
print list2
結果是:

按照負數索引值獲取內容的代碼如下:
def f3():
list1 = [1,2,3,4,5,6,7,8]
list2 = list1[-8:-3:1]
print list1
print list2
結果是:

在字符串中使用分片的情況如下:
def f4():
str1 = "good idea!"
str2 = str1[1:6:2]
print str1
print str2
結果是:

以上是關於get_slice的記錄,下面是set_slice的記錄:
def f5():
list1 = [1,2]
list1[1:4] = [11,3,4]
print "替換部分元素,並增加部分元素后:", list1
list2 = [1,2,3,4,5]
list2[2:4] = []
print "置空某些元素:", list2
list3 = [1,2,3,4]
list3[1:7] = [111]
print "右索引大於總長度,且右邊的列表比左邊的索引差值小:", list3
list4 = [1,2,3,4]
list4[2:3] = [5,6,7,8,9,10]
print "右邊列表比左邊的索引差值大:",list4
結果是:

