Deep understanding of slicing principles and deep understanding of Slicing

Source: Internet
Author: User

Deep understanding of slicing principles and deep understanding of Slicing

A data element is obtained by specifying the subscripts or by specifying the subscript range. This access sequence is called slice. In some cases, it is also called a shard.

For details about how slice works, refer to my previous article: How slice works in Python.

First, analyze the slice operation from the bottom layer:
Internally, _ getitem __,__ setitem __,__ delitem _ and slice functions are called. The slice function is related to the range () function.
The key passed to the slice is a special slice object. This object has attributes that can describe the request slice orientation,
For example:

a = [1,2,3,4,5,6]x = a [ 1 : 5 ]                # x = a.__getitem__(slice( 1, 5, None))a [ 1 : 3 ] = [10, 11, 12 ]    # a.__setitem__(slice(1, 3, None), [ 10, 11, 12 ])del a [ 1 : 4 ]                # a.__delitem__(slice(1, 4, None))


Slice Operation Method: Con [start_index: end_index: step]
Start_index: indicates the first element object. The default positive index position is 0. The default negative index position is-len (con)
End_index: indicates the last element object. The default positive index position is len (con)-1, and the default negative index position is-1.
Step: indicates the step size of the value. The default value is 1, and the step value cannot be 0.
For the sequence, both the index and step size have positive and negative values, indicating the values in the left and right directions respectively. The positive direction of the index is from left to right. the start position is 0. The negative direction is from right to left. the start position is-1.
Therefore, the index range of any sequence structure data is a continuous integer from-len (con) to len (con)-1.

Slice Operation Type:
Con [start_index]: returns the object whose index value is start_index. Start_index is an arbitrary integer between-len (con) and len (con)-1.
Con [start_index: end_index]: returns a continuous object from start_index to the end_index-1.
Con [start_index: end_index: step]: returns a continuous object whose index value is between start_index and end_index-1 and whose difference from start_index can be divisible by step.

Con [start_index:]: Default end_index, which indicates that the last object in the sequence starts from start_index.
Con [: end_index]: Default start_index, representing the fragment from the first object in the sequence to the end_index-1.
Con [:]: The default start_index and end_index indicate the complete fragment from the first object to the last object.
Con [: step]: Default start_index and end_index indicate the value of the rule that can be divisible by step for the entire sequence.

Slice usage:

#1. case Study on step values >>> lst = range (10) >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # case 1 >>> lst [: 1] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst [0: len (lst)] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst [-len (lst ): -1]. append (lst [9]) >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # case 2> lst [:-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> lst [-1:-10:-1] [9, 8, 7, 6, 5, 4, 3, 2, 1] # case 3> lst [-] []> lst [-] [] Lst [-] is equivalent to lst [-]. The return value is null because the index of the last element cannot be found. # Case 4 >>> lst [-1: 1:-1] [9, 8, 7, 6, 5, 4, 3, 2] starting from an element whose subscript is-1, the element whose index is 1 (2) is sliced in reverse direction ). Equivalent to: lst [-1:-len (lst) + 1:-1] >>> lst [-1:-9:-1] [9, 8, 7, 6, 5, 4, 3, 2] #2. case Study on start_index and end_index >>> lst = [1, 2, 3, 4, 5, 6] # the upper bound of lst is 0, the lower bound is 6 >>> lst [-10] #-10 exceeds the upper bound, and 10 exceeds the lower bound. It is equivalent to lst [0: 6] and Returns [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] >>> lst [-10:-20] #-10 and-20 both exceed the upper limit, automatically obtain the upper bound, which is equivalent to lst [0: 0]. The return value [] []> lst [10: 20] #10 and 20 both exceed the lower bound. The lower bound is automatically obtained, equivalent to lst [], return [] []> lst [: 100] # start default: Returns [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] >>> lst [0:] # end default: Get from start to end, return [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] #3. start_index: end_index: step comprehensive case >>>> lst = range (100) >>> lst [: 10] # obtain the first 10 numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst [-10:] # obtain the last 10 numbers [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> lst [] # Number of 11-20 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> lst [: 20: 2] # Take the first 20 numbers, and take 1 [0, 2, 4, 6, 8, 10, 12 at every two, 14, 16, 18] >>> lst [: 5] # Take all numbers, take one [0, 5, 10, 15, 20, 25 at intervals of five, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] >>> lst [:] # write nothing, indicates that the original copy list [0, 1, 2, 3, 4, 5, 6 ..., 98, 99] >>> lst [:] # Do not write anything, it means to copy the list as is [0, 1, 2, 3, 4, 5, 6 ..., 98, 99] # Use the range Function to generate a number of 1-1,100, take a multiple of 3, and take the first 10 >>> [I for I in range () [2 :: 3] [: 10] [3, 6, 9, 12, 15, 18, 21, 24, 27, 30] # Use the range Function to generate a number ranging from 1, take a multiple of 3 and take the last ten >>> [I for I in range (1,100) [2: 3] [-10:] [72, 75, 78, 81, 84, 87, 90, 93, 96, 99] # Take a multiple of 7 from range (1,100) >>> [x for x in range (1,100) [6: 7] [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]

Compared with reverse, the slicing method does not change the structure of the list, so this is a useful technique in practical application.

References:
Python slicing: https://www.cnblogs.com/weidiao/p/6428681.html
Understanding slice in Python: http://blog.csdn.net/u011242657/article/details/56289429

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.