Slicing operations and techniques for Python sequences

Source: Internet
Author: User

slice operations for data with a sequence structure, the method for slicing is: Consequence[start_index:end_index:step]. Start_index: Represents the first element object, the positive index position defaults to 0, and the negative index position defaults to-Len (Consequence) End_index: Represents the last Element object, the positive index position defaults to Len (consequence)-1; Negative index position defaults to-1. Step: Indicates the step size of the value, default is 1, the step value cannot be 0. [note] for the sequence structure data, both index and step have a plus or minus two values, representing the left and right two direction values respectively. The positive value of the index is from left to right, the starting position is 0, the negative direction is from right to left, and the starting position is-1。 Therefore, the index range of any sequence structure data is-len (consequence) to Len (consequence)-1a contiguous integer within the range. The slice operation will intercept fragments of successive objects in the sequence at a given index and step size, and a single index return value can be considered as a contiguous fragment containing only one object. The process of slicing is to start with the first object you want, and end with the first object you don't want. The first object that you want to be a continuous object to the first unwanted object is all you want. Therefore, in Consequence[start_index:end_index], the slice contains consequence[start_index], but does not include Consequence[end_index]. Operation type of slice: Con[start_index]: Returns an object with an index value of Start_index. Start_index for-len (Con) to Len (Con)-any integer between 1. Con[start_index:end_index]: Returns a contiguous object with an index value between Start_index and End_index-1. Con[start_index:end_index:step]: Returns an index value of Start_index to End_index-1, and the difference between the index value and the Start_index can be divisible by the step of the continuous object. Con[start_index:]: Default End_index, which represents the last object from the beginning of Start_index to the sequence. con[: End_index]: Default Start_index, representing the first object from the sequence to the End_index-a fragment between 1. con[:]: Default Start_index and End_index, which represent a complete fragment from the first object to the last object. Con[::step]: Default Start_index and End_index, indicating that the entire sequence is evaluated by the rules that the index can be divisible by the step. When you use a single index to address a sequence addressing a value, the index value that you enter must be-len (consequence) to Len (consequence)-1Value, otherwise an error indicates that the index value is out of range.
string, Unicode, buffer, xrange. Where Xrange is special, it is a generator, and several other types have some sequence attributes that are not suitable for it.

Slicing operations and techniques for Python sequences

Related Article

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.