Python slice Detailed

Python slice Detailed

      Process using the python problem solving, often we encounter partial extract value from an object. "Slice" operation it is a powerful weapon designed to achieve this goal. Theoretically, as long as the proper conditional expression may be achieved by any single or multiple target slicing cut. The basic syntax for slicing operation is relatively simple, but if you do not completely clear internal logic, but also very prone to error, and this error is sometimes hidden too deep, difficult to detect. This article summarizes a detailed example of various situations slicing operations, list types are hereinafter as subjects, the conclusion may be extended to other objects can be sliced.
 

First, indexing can slice objects Python

Comprising: positive and negative index index of two parts, as shown, to a list object a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Example:

Second, the general mode of operation of the slice Python

A complete expression contains two slice ":", for separating the three parameters (start_index, end_index, step). When only a ":", the default parameters of the third step = 1; when a ":" is not, start_index = end_index, indicates that the element cut start_index specified.
1 slicing basic expressions: object [start_index: end_index: step ]

step: positive and negative numbers are available, the absolute value of which determines the cut when the data 'step ", and determines the sign of the" cut out direction ", n indicates" left to right "value, it indicates a negative" from right to left "value. when the step is omitted, the default is 1, i.e., from left to right in steps of a value." cut orientation is important! "" Cut direction is very important! "" Cut direction is very important! "The important thing to say three times!

start_index: indicating a start index (containing the index value); Omit this argument, from the object "endpoint" start value, as is or from the "end" from the beginning "starting point", the positive and negative parameters determined by step , step is positive from the "starting point" to start, start from a negative "end."

end_index: indicates termination index (the index value is not included); when the argument is omitted, taking data up indicates "end", as is the "starting point" or the "end point", the same positive and negative parameter determined by the step, step is positive until the "end" is negative until the "starting point"

Three, Python detailed example of slicing operation

The following are examples of the object list a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Example:

1 >>>a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1. cut individual elements

. 1 >>> A [0]
 2 >>> 0
 . 3 >>> A [-4 ]
 . 4 >>>. 6
 . 5 when the index is only one number, an element represents a cut.

2. cut the complete object

. 1 >>> A [:] # left to right 
2 >>> [0,. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9 ]
 . 3 >>> A [::] # left right 
. 4 >>> [0,. 1, 2,. 3,. 4,. 5, 6,. 7,. 8, 9 ]
 . 5 >>> A [:: -. 1] # right to left 
6 >>> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

3. start_index and end_index are all positive (+) index case

. 1 >>> A [. 1:. 6 ]
 2 >>> [. 1, 2,. 3,. 4,. 5 ]
 . 3 STEP =. 1, from left to right value, start_index = 1 to end_index = 6 represent the same left to right value.
. 1 >>> A [. 1:. 6: -1 ]
 2 >>> []
 . 3  outputs an empty list, indicating that the data did not get to.
. 4 STEP = -1, the values determined from right to left, while start_index = 1 end_index = 6 to determine the value from left to right, both conflicts, it is empty.
. 1 >>> A [. 6: 2 ]
 2 >>> []
 . 3  empty list same output.
. 4 STEP =. 1, from left to right decision value, and start_index = 6 end_index = 2 to determine the values from right to left, both conflicts, it is empty.
. 1 >>> A [:. 6 ]
 2 >>> [0,. 1, 2,. 3,. 4,. 5 ]
 . 3 STEP =. 1, from left to right represents the value, while start_index omitted, showing the beginning end, so here the endpoint is "starting point", i.e., the "starting point" to the value zero has been taken end_index = 6 (which are not included).
. 1 >>> A [:. 6: -1 ]
 2 >>> [. 9,. 8,. 7 ]
 . 3 STEP = -1, from right to left value, while start_index omitted, showing the beginning end, so here the endpoint It is the "end point", i.e. the "end" to the value of 9 starts to take up end_index = 6 (which are not included).
. 1 >>> A [. 6 :]
 2 >>> [. 6,. 7,. 8, 9 ]
 . 3 STEP =. 1, from left to right value from start_index = 6, has been taken to the "end" value 9.
. 1 >>> A [. 6 :: -. 1 ]
 2 >>> [. 6,. 5,. 4,. 3, 2,. 1 , 0]
 . 3 STEP = -1, the values from right to left, beginning from start_index = 6, He has been taken to the "starting point" 0.

4. start_index end_index full and negative (-) in the case where the index

. 1 >>> A [-1: -6 ]
 2 >>> []
 . 3 STEP =. 1, from left to right value, and to start_index = -1 = end_index - . 6 determines the values from right to left, two those contradictions, it is empty.
4 Index -6 -1 to the right (as shown above)
. 1 >>> A [-1: -6: -1 ]
 2 >>> [. 9,. 8,. 7,. 6,. 5 ]
 . 3 STEP = -1, the values from right to left, start_index = -1 to end_index = - 6 is the same value from right to left.
4 Index-1 to the right. 6 (above)
. 1 >>> A [-6: -1 ]
 2 >>> [. 4,. 5,. 6,. 7,. 8 ]
 . 3 STEP =. 1, from left to right argument, to the start_index = -6 = end_index - . 1 Similarly from left to right value.
4 Index -6 -1 on the left (above)
. 1 >>> A [: -. 6 ]
 2 >>> [0,. 1, 2,. 3 ]
 . 3 STEP =. 1, the value from left to right, from the "starting point" to the beginning has been taken end_index = -6 (the point not included).
. 1 >>> A [: -. 6: -1 ]
 2 >>> [. 9,. 8,. 7,. 6,. 5 ]
 . 3 STEP = -1, the values from right to left, from the "end" has been taken to start end_index = 6 (which are not included).
. 1 >>> A [-6 :]
 2 >>> [. 4,. 5,. 6,. 7,. 8,. 9 ]
 . 3 STEP =. 1, from left to right value from start_index = -6, has been to take " end".
. 1 >>> A [-6 :: -. 1 ]
 2 >>> [. 4,. 3, 2,. 1 , 0]
 . 3 STEP = -1, the values from right to left, starting from start_index = -6, has been taken to "starting point."

In the case of hybrid index - 5. start_index end_index and positive (+) and negative ()

. 1 >>> A [. 1: -6 ]
 2 >>> [. 1, 2,. 3 ]
 . 3 start_index. 1 in end_index = left = -6, and therefore the value from left to right, and step = 1 also determines from left to right value, so the correct result
. 1 >>> A [. 1: -6: -1 ]
 2 >>> []
 . 3 start_index. 1 in end_index = left = -6, and therefore the value from left to right, but step = - the right to determine the left value, both contradictory and therefore empty.
. 1 >>> A [-1: 6 ]
 2 >>> []
 . 3 start_index = -1 in the right end_index = 6, and therefore the value from right to left, then decision step = 1 but the values from left to right both conflicts, so empty.
. 1 >>> A [-1: 6: -1 ]
 2 >>> [. 9,. 8,. 7 ]
 . 3 start_index = -1 in the right end_index = 6, and therefore the value from right to left, and step = -1 also determines the value from right to left, so the results are correct.

6. The multilayer slicing operation

. 1 >>> A [:. 8] [2:. 5] [-. 1 :]
 2 >>> [. 4 ]
 . 3  corresponds to:
 . 4 A [:. 8] = [0,. 1, 2,. 3,. 4,. 5, . 6,. 7 ]
 . 5 A [:. 8] [2:. 5] = [2,. 3,. 4 ]
 . 6 A [:. 8] [2:. 5] [-. 1:] = [. 4 ]
 . 7 theoretically unlimited multiple layer slicing operation, as long as a non-return null objects to be sliced.

7. The three parameters can be used slicing operation expression

1 >>>a[2+1:3*2:7%3]
2 >>> [3, 4, 5]
3 即:a[2+1:3*2:7%3] = a[3:6:1]

8. Other objects slicing

In front of the slicing operation to the object list as an example, but actually data of many types of slicing operation, comprising a tuple, string and the like.

1 >>> (0, 1, 2, 3, 4, 5) [: 3 ]
 2 >>> (0, 1, 2 )
 3 -tuples slicing operation
. 1 >>> ' ABCDEFG ' [:: 2 ]
 2 >>> ' ACEG ' 
. 3 string slicing operation
1 >>> for I in Range (1,100) [2 ::. 3] [-. 5 :]: 
 2         Print (I)
 . 3 >>> 87
 . 4 90
 . 5 93
 . 6 96
 . 7 99
 . 8 is the use range () function to generate a integer-99, and then from start_index = 2 (i.e., 3) to start step = 3 value, until the end, then take the last five in number in the new sequence.

Fourth, the common slicing operation

1. Take even positions

1 >>>b = a[::2]
2 [0, 2, 4, 6, 8]

2. Take odd positions

1 >>>b = a[1::2]
2 [1, 3, 5, 7, 9]

3. Copy the entire object

1 >>>b = a[:] #
2 >>>print(b) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3 >>>print(id(a)) #41946376
4 >>>print(id(b)) #41921864
5 6 >>>b = a.copy()
7 >>>print(b) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8 >>>print(id(a)) #39783752
9 >>>print(id(b)) #39759176

Note that: [:] and .copy () are "shallow copy", copy only the outermost element, the inner nested elements shared by reference, rather than separate memory allocation, if the need to completely copy is required a "deep copy" mode, in the following example:

. 1 >>> A = [1,2, [ ' A ' , ' B ' ]]
 2 >>> Print ( ' A = {} ' .format (A))
 . 3 >>> B = A [:]
 . 4 b >>> [0] =. 9 # modify b outermost element of the 1 becomes. 9 
. 5 >>> b [2] [0] = ' D '  # modify the embedded layer of the element b 
6 >>> Print ( ' A = {} ' .format (A))
 . 7 >>> Print ( ' B = {} ' .
format(b))
 8 >>>Print ( ' ID (A) = {} ' .format (ID (A)))
 . 9 >>> Print ( ' ID (B)} = { ' .format (ID (B)))
 10 A = [. 1, 2, [ ' a ' , ' B ' ]] # original a 
. 11 a = [. 1, 2, [ ' D ' , ' B ' ]] # B after a modified inner elements D, a is a also becomes the D, description of the internal shared nested elements, an outer element but not changed. 
12 is B = [. 9, 2, [ ' D ' , ' B '
 id(a)=38669128
14 id(b)=38669192

4. Modify the individual elements

1 >>>a[3] = ['A','B']
2 [0, 1, 2, ['A', 'B'], 4, 5, 6, 7, 8, 9]

5. Insert the element in a certain position

1 >>>a[3:3] = ['A','B','C']
2 [0, 1, 2, 'A', 'B', 'C', 3, 4, 5, 6, 7, 8, 9]
3 >>>a[0:0] = ['A','B']
4 ['A', 'B', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

6. Alternatively part of the element

1 >>>a[3:6] = ['A','B']
2 [0, 1, 2, 'A', 'B', 6, 7, 8, 9]

V. Summary

(A) start_index, end_index, step three can be both positive, the same is negative, negative, or mixed. But must follow a principle, that is: when the actual position indicated at the left start_index end_index, the value from left to right, the number must be a positive step at this time (similarly represented from left to right); when the actual position represented in end_index start_index when right, right to left represents the value at this time step must be negative (right to left represent the same), i.e., the order of the values ​​of both must be the same.

(B) when start_index end_index or omitted, and the starting index value index determined by the positive and negative termination of the step, this does not have a value direction of contradiction (i.e., does not return empty list []), but the positive and order to get a negative result is reversed because of a left a right.

(C) step is positive and negative must be considered, especially when the step is omitted. Such as a [-1:], easily mistaken for the "destination" starts to get up "starting point", i.e. a [-1:] = [9, 8, 7, 6, 5, 4, 3, 2 , 1, 0], but in fact a [-1:] = [9] (not note 9), when the step is omitted because the step = 1 represents a value from left to right, while the start index itself start_index = -1 is the right-most elements of the object, and then did not have the right data, so the results 9 contains only one element.

(D) Note: "Take a single element (without": ")", the return element is an object whose type is determined by the type of element itself, regardless of the parent object, such as above a [0 ] = 0, a [-4] = 6, elements 0 and 6 are "numeric", is the parent object a "list" type; "serial sections taken (with a": ")", the return result and mother objects of the same type, even of serial sections cut contains only one element, as above a [-1:] = [9], returns a list containing only the element "9", rather than numeric "9"



Author: Maldives Maldives
link: https: //www.jianshu.com/p/15715d6f4dad

 

Guess you like

Origin www.cnblogs.com/mtn007/p/12074952.html