Takes the specified range of actions for a list or tuple. can use slices (slice), very useful
1. List: variable array
L=[' A ', ' B ', ' C ', ' d ', ' e '] >>> L[0:3] #从第0个开始, to the end of 3rd place, not including 3rd place, that is, 0,1,2 [' A ', ' B ', ' C ']>>> L[1:2] # Starting from the 1th position, to the end of the 2nd place, excluding 2nd digit, that is, the 1th bit [' B ']>>> L[:2] #从第0位开始, take 2 bits with the same meaning as the 1th example, to the 2nd bit, not including the 2nd bit, that is No. 0, 1 bits [' A ', ' B ']>>> L[1:3] #从第1位开始, to 3rd place, not including 3rd place, that is 1th, 2 bits [' B ', ' C ']>>> l[-1] #倒数第一个位置 ' e ' >>> l[-2:] #倒数2位 [' d ', ' E ']> >> l[-2] #倒数第2位 ' d '
Instance:
>>> l=list (Range) #生成一个0-99 of List>>> l[0, 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 8, 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, -------------------------------------------0, 1, 2, 94, 98, 99]>>> L[:10] #取第0到第9位 3, 4, 5, 6, 7, 8, 9]>>> l[-10:] #取后10位 [All-in-a-94, the----------] , one, a, a, a, (+), 19]>>> l[11:20] #取第11到第19位 [One, A, a, a, a, a, a, *,], 19]>>> l[:1 0:2] #取第0到第9位, take once every 2 bits [0, 2, 4, 6, 8]>>> L[::5] #取从0位开始, each 5 fetch one [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65 , 95]>>>, l[, I, A, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 and 12, 13, 14, 15, 16. , 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, 81, 82, 83 , the 94, the 98, the 99]>>>, the ",", "
2, tuple non-variable group
But you can also use slices, but the result of the operation is still a tuple
>>> r= (1,2,3,4,5,6) >>> R[:3] (1, 2, 3)
A string can also be considered a list, each character as an element, but the result of the operation is still a string
>>> w= ' ABCDEFG ' >>> w[:3] ' abc ' >>> w[::2] ' Aceg '
python-Tile Instance