python basis - sliced

slice

l format: [Start: End: STEP]
l the Start: start index, starting from 0, -1 indicating the end
l End: End index
l Step: step
l end-start = a positive number, left to right value reverse = negative value when
l Note: the end result does not contain the slice index, i.e., does not include the last one, -1 is the last position index

>>> s = list (range (11 )) # generates a list 
>>> S 
[0,. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10 ] 

>>> A [. 1: 3] colon sections # 1, 2 is on the right section, taken only to 1,2. 
[1, 2 ] 
>>> S [0:. 5] in steps # 1 is not written default 
[0, 1, 2 ,. 3,. 4 ]  >>> S [2:] # End default value is not written to take a final  [2,. 3,. 4,. 5,. 6,. 7,. 8,. 9, 10 ]  >>> S [2 : 9: 2 ] of step # 2 [2, 4, 6, 8 ] S to take down >>> [-1: -8: -2 ] [10, 8, 6, 4 ] Example string sections: removed >>> = S roadoo "! gloryroad IS Good" >>> S [5:10 ] 'Road' >>> S [-3: -5: -1 ] 'OO' >>> Result = S [. 5: 10] + s [-3: -5 : -1] # string concatenation with >>> + Result 'Road OO' >>> S [5:20] # is not given cross-border sections' road is good!'

 

Python in line with the sequence of an ordered sequence of support sections (slice)
such as: a list of characters, Ganso (except dictionaries), Example:

>>> s={'a':1,'b':2,'c':3}
>>> s[1:3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'slice'

 

Guess you like

Origin www.cnblogs.com/wenm1128/p/11557993.html