Preface
One 、 sliced 2 Two index directions
Two 、 How to slice
3、 ... and 、 Some basic operations
1、 Cut a single object
2、 Cut the entire list
3、 step step Positive and negative value Some cases that determine the direction of the index
Four 、 Advanced operation
1. Modify an element value
2. Replace element values
3、 Insert element values
4、 Select slice location
summary
This article is from :link.
PrefaceRecent learning python section , Sometimes I am enlightened and sometimes I am in a fog , Today, I finally found out python section Where I often wonder , Make a note of , Easy to check later .
One 、 sliced 2 Two index directions If you master the slicing method , You can cut all the target values at will , However, the target objects that can be sliced are 2 Two index directions : Positive index 、 Negative index . As shown in the figure below ,
With a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] give an example :
This string a The positive index of is from left to right , The head subscript is 0, Tail subscript is 9;
Its negative index is from right to left , The head subscript is -1, Tail subscript is -10.
Two 、 How to sliceTo successfully complete the slice , Then you need to include 2 individual ‘ :’ Symbol , as well as 3 Parameters , Namely start_index、end_index、step, A complete slice expression is as follows :
Basic expression of slice operation :object[start_index : end_index : step]
1、 Start index start_index : It indicates the position where the slice is started , And the slice result contains the value under the index ;
2、 Terminate index end_index: Indicates the position of the end slice , And the slicing result does not contain the value under the index ;
3、 step step : Represents the cut step value , Positive but negative ,step Positive timing , Index direction is positive ,step When it is negative , The index direction is negative , When step When the value is omitted , The default value is 1.
Bear in mind : The positive or negative step size determines the slice direction , It's very important !!
When not written start_index、end_index The value of , By default , Start at the end of the index
3、 ... and 、 Some basic operationsThe following example objects are lists a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1、 Cut a single objectThe cut index is 3 The value corresponding to the position of :
2、 Cut the entire listWhen the value is not filled in , Start at the end
3、 step step Positive and negative value Some cases that determine the direction of the indexstep=1, From left to right , contain start_index =1 The index itself contains values , Does not include terminating index end_index =5 The value contained in itself .
// An highlighted block>>> a [1: 5: ][1, 2, 3, 4]>>> # step step = 1, The default value is , Positive index direction from left to right
step = -1, From right to left , Still does not contain the termination index =5 The value contained in itself
// An highlighted block>>> a [ : 5 :-1][9, 8, 7, 6]>>> # step step= -1, Negative index , The starting index is not written , It means starting from the endpoint, from right to left , The termination index is 5
The index directions are contradictory , Value is empty
// An highlighted block>>> a [7 : 2 ][]>>> # This is because the starting index is 7 Terminate index to 2, The direction is from right to left ,> But step size step The default is 1, Indicates a positive index , From left to right , The two directions are contradictory, and the value is null
When the index value is negative , All with step The value is positive or negative , Determine the index direction !
// An highlighted block>>> a [ -6 : :][4, 5, 6, 7, 8, 9]>>> # From start_index = -6, In steps of 1, From left to right
According to what we have learned , The slice result contains the value of the starting index position , But it does not contain the value of the ending index position , This means that a specific termination index is given , as follows :
// An highlighted block>>> # Given an explicit termination index location , The result does not contain the value of the termination index itself, and the following does not contain end_index = 5 The value of the position >>> a [ : 5 : -1 ][9, 8, 7, 6]
This case column contains the value of the end index position , Refer to the previous article to add comparison .
in other words , Specifies the specific stop index location , Then the value does not include the value at this position , If not specified , The default termination index is at the endpoint , Then the value includes the value of the position
// An highlighted block>>> a [ 5: : -1][5, 4, 3, 2, 1, 0]>>> # No specific end index location was specified , So it ends at the endpoint by default , So include the value at the endpoint , namely 0
Four 、 Advanced operation 1. Modify an element value Directly bring the modified value into the index
// An highlighted block>>> a [ 2 ] = [ 'A' ]>>> print (a)[0, 1, ['A'], 3, 4, 5, 6, 7, 8, 9] # Assign a value directly at the index to be modified , Index values = 2 Is changed to ['A']
2. Replace element values Start from the starting index , Contains the value of the starting index itself , Insert values into , Then the index itself and subsequent values will be terminated , Just follow the inserted value , Does not contain the termination index itself . Instead of indexing 3 and 5 The elements between are case studies
contain “ Starting index =3 ” The element itself , It doesn't contain ” Terminate index = 5“ The element itself , The number of elements should be 2 individual .
(1) Given the number of elements to be replaced = 2
// An highlighted block>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ 3: 5] = [ 'b', 'c']>>> print (a)[0, 1, 2, 'b', 'c', 5, 6, 7, 8, 9]>>> # Alternative writing , Replace the value between the start index itself and the index before the end index ,> The value of the terminating index itself is not replaced
(2) Given the number of elements to be replaced > 2 individual
// An highlighted block>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ 3: 5 ] = [ 'b', 'c', 'd', 'e']>>> print (a)[0, 1, 2, 'b', 'c', 'd', 'e', 5, 6, 7, 8, 9]>>> # Insert writing , The index from is 3 The insertion value is brought in at the place of , Carry in the inserted value , Set the index value to 5 The value itself and the values that follow it are arranged immediately after each other
(3) Given the number of elements to be replaced < 2.
// An highlighted block>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ 3: 5 ] = [ 'b' ]>>> print (a)[0, 1, 2, 'b', 5, 6, 7, 8, 9]>>> # Replaced the starting index = 3 It's worth it ,> Terminate index =5 And subsequent values follow the inserted value ,> Indexes =4 The value at is accompanied by the index = 3 The values of are replaced together
3、 Insert element values Slice here , The most perplexing thing is that you don't understand the replacement element value 、 And insert element values , Now it dawns , The principle of replacement is described above , The next step is to insert the element value, which is actually very simple , Just keep , Terminate index 、 The starting index is the same value And then assign values
(1) Insert an element
// An highlighted block>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [6:6 ] = ['c', 'd', 'e', 'f','g']>>> print (a)[0, 1, 2, 3, 4, 'b', 'c', 'd', 'e', 'f', 'g', 5, 6, 7, 8, 9]>>> # Insert multiple elements , The value and number of other original elements do not change
(2) Insert multiple elements , Even strings
// An highlighted block>>> a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print (a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ 3: 3] = [ 'hello , world!']>>> print (a)[0, 1, 2, 'hello , world!', 3, 4, 5, 6, 7, 8, 9]
4、 Select slice location (1) Slice the positions with even index values
// An highlighted block>>> a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print(a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ : : 2][0, 2, 4, 6, 8]>>> # Select a position with an even index value to slice
(2) Select the positions with odd index values to slice
// An highlighted block>>> a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> print(a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> a [ 1: :2][1, 3, 5, 7, 9]>>> # Select the positions with odd index values to slice
Sum up , Today is the day to learn python A summary of the slices , Record complete !
summaryThis is about python This is the end of the article on slicing methods , More about python Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !