關于list的insert函數
list#insert(ind,value)在ind元素前面插入value
首先對ind進行預處理:如果ind<0,則ind+=len(a),這樣一來ind就變成了正數下標
預處理之后,
當ind<0時,ind=0,相當于頭部插入
當ind>len(a)時,ind=len(a),相當于尾部插入
切片實例
Python中的列表切片非常靈活,要根據表象來分析它的內在機理,這樣用起來才能溜。
下標可以為負數有利有弊,好處是使用起來更簡便,壞處是當我下表越界了我也不知道反倒發生奇奇怪怪的錯誤。
1
2
3
4
5
6
7
8
9
10
|
print str [ 0 : 3 ] #截取第一位到第三位的字符 print str [:] #截取字符串的全部字符 print str [ 6 :] #截取第七個字符到結尾 print str [: - 3 ] #截取從頭開始到倒數第三個字符之前 print str [ 2 ] #截取第三個字符 print str [ - 1 ] #截取倒數第一個字符 print str [:: - 1 ] #創造一個與原字符串順序相反的字符串 print str [ - 3 : - 1 ] #截取倒數第三位與倒數第一位之前的字符 print str [ - 3 :] #截取倒數第三位到結尾 print str [: - 5 : - 3 ] #逆序截取 |
可見,列表的下標有三個參數:beg(起始下標),end(終止下標),delta(變化量)
- 當delta小于0時,beg默認為len(array)-1,end默認為開頭之前。
- 當delta大于0時,beg默認為0,end默認為最末之后。
- 當delta未給出時:delta默認為1
這個代碼示例演示了大概原理,學習一件事物,先學習它的表象,然后分析它的內在實現,最后查看源代碼仔細推敲它到底是怎么實現的。
需要注意的是,列表切片產生的是列表的副本,與原列表不是同一份空間。
1
2
3
4
|
x = [ 1 , 2 , 3 ] y = x[:] x[ 0 ] = - 1 print (y) #輸出[1,2,3] |
列表切片寫操作
接下來探究切片的寫操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
>>> x = [ 1 , 2 , 3 , 4 , 5 ] >>> x[ 2 : 0 ] = 100 #在2后面插入若干個元素,應該用列表 Traceback (most recent call last): File "<stdin>" , line 1 , in <module> TypeError: can only assign an iterable >>> x[ 2 : 0 ] = [ 100 ] >>> x [ 1 , 2 , 100 , 3 , 4 , 5 ] >>> del x[ 2 : 3 ] #刪除切片 >>> x [ 1 , 2 , 3 , 4 , 5 ] >>> x[ 2 : 1 ] = [ 100 ] #對于切片x[from:to],會進行預處理to=max(from+1,to) >>> x [ 1 , 2 , 100 , 3 , 4 , 5 ] >>> del x[ 2 : 0 ] #對于切片del操作,如果from>to,不執行任何操作 >>> x [ 1 , 2 , 100 , 3 , 4 , 5 ] >>> del x[ 2 : 1 ] >>> x [ 1 , 2 , 100 , 3 , 4 , 5 ] >>> del x[ 2 : 3 ] >>> x [ 1 , 2 , 3 , 4 , 5 ] >>> x[ 2 : 4 ] = None Traceback (most recent call last): File "<stdin>" , line 1 , in <module> TypeError: can only assign an iterable >>> x[ 2 : 4 ] = [ None ] >>> x [ 1 , 2 , None , 5 ] |
對列表切片進行深入理解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
def between(beg, end, mid): # 判斷mid是否位于begin和end之間 return end > mid > = beg or end < mid < = beg def get_slice(a, beg, end, delta = 1 ): # 數組切片get方式 if delta = = 0 : raise ValueError( "slice step cannot be 0" ) # 將負數下標轉化一下 if beg < 0 : beg + = len (a) if end < 0 : end + = len (a) # 如果轉化完成之后依然不在合法范圍內,則返回空列表 if beg < 0 and end < 0 or beg > = len (a) and end > = len (a): return [] # 如果方向不同,則返回空列表 if (end - beg) * delta < = 0 : return [] # 將越界的部分進行裁剪 beg = max ( 0 , min (beg, len (a) - 1 )) end = max ( - 1 , min (end, len (a))) ans = [] i = beg while between(beg, end, i): ans.append(a[i]) i + = delta return ans def set_slice(a, li, beg, end, delta = 1 ): if delta = = 0 : raise ValueError( "slice step cannot be 0" ) if delta = = 1 : # 如果delta==1,那么li的長度可以隨意 if beg < 0 : beg + = len (a) if end < 0 : end + = len (a) beg = max ( 0 , min (beg, len (a) - 1 )) end = max ( - 1 , min (end, len (a))) for i in range (beg, end): del a[beg] for i in reversed (li): a.insert(beg, i) else : # delta!=1,相當于替換 if len (get_slice(a, beg, end, delta)) ! = len (li): raise ValueError( "array don't match" ) if len (li) = = 0 : return if beg < 0 : beg + = len (a) if end < 0 : end + = len (a) beg = max ( 0 , min (beg, len (a) - 1 )) # 用li中的全部元素逐一替換 for ind, value in enumerate (li): a[ind * delta + beg] = value def test_getSlice(): a = list ( range ( 10 )) import random for i in range ( 10 ): beg = random.randint( - 15 , 15 ) end = random.randint( - 15 , 15 ) delta = 0 while delta = = 0 : delta = random.randint( - 15 , 15 ) print ( len (get_slice(a, beg, end, delta)) = = len (a[beg:end:delta]), beg, end, delta) def test_setSlice(): import random for i in range ( 10 ): a = list ( range ( 10 )) beg = random.randint( - 15 , 15 ) end = random.randint( - 15 , 15 ) delta = 0 while delta = = 0 : delta = random.randint( - 5 , 5 ) sz = len (a[beg:end:delta]) if delta = = 1 : sz = random.randint( 0 , 4 ) li = [random.randint( 0 , 100 ) for i in range (sz)] set_slice(a, li, beg, end, delta) mine = a a = list ( range ( 10 )) a[beg:end:delta] = li print (a = = mine) test_setSlice() |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/weidiao/p/6428681.html