Slicing operations and techniques for Python sequences

Source: Internet
Author: User
Tags integer min range in python

Sequence

A sequence (consequence) is a data structure in Python that obtains objects in a sequence based on an index.

Python contains six types of built-in sequence classes: list, tuple, string, Unicode, buffer, xrange. Where Xrange is special, it is a generator, and several other types have some sequence characteristics that are not appropriate for it.

Generally, data types with sequential structures can be used: index, Len, Max, Min, in, +, *, slices. Such as:

>>> a = ' Iloveyou '
>>> len (a)
>>> Max (a)
' y '
>>> min (a)
' I '
>>> bool (' O ' in a)
True
>>> A + a
' iloveyouiloveyou '
>>> a*3
' iloveyouiloveyouiloveyou '
> >> A[1:4]
' lov '
>>> a.index (' y ')
>>> a[5]
' y '

Slicing operations

For data with a sequential 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; The negative index position defaults to-1.

Step: Represents the size of the value, the default is 1, and the step value cannot be 0.

[note] for the sequence structure data, the index and step are both positive and negative two values, respectively, representing the left and right two direction values. The square of the index is taken from the left to the right, starting at 0, the negative direction from the right to the left, and the starting position is-1. Therefore, the index range of any sequence structure data is a continuous integer in the range of-len (consequence) to Len (consequence)-1.

The slice operation will intercept fragments of contiguous objects in the sequence, according to the given index and step size, and a single index return value can be treated as a contiguous fragment containing only one object.

The process of slicing begins with the first object you want, to the end of the first unwanted object. The first object you want is the contiguous object to the first unwanted object that you all want.

So in Consequence[start_index:end_index], the slice contains consequence[start_index], but not consequence[end_index].

Action type of slice:

Con[start_index]: Returns an object with an index value of Start_index. Start_index is an arbitrary integer between-len (Con) and Len (Con) 1

.
Con[start_index:end_index]: Returns a contiguous object with an index value of Start_index to End_index-1.
Con[start_index:end_index:step]: A continuous object that returns the index value between Start_index and End_index-1, and the difference between the index value and

the Start_index can be divisible by the step.
    
Con[start_index:]: The default end_index, which means starting from Start_index to the last object in the sequence.
con[: End_index]: Default Start_index, representing fragments from the first object in the sequence to the end_index-1.
con[:]: Default Start_index and End_index, representing the complete fragment from the first object to the last object.
Con[::step]: Default Start_index and End_index that represent the value of a rule that the entire sequence is divisible by the step by the index.

When using a single index to address sequence addressing, the index value you enter must be a value between-len (consequence) to Len (consequence)-1, otherwise the index value is out of range. Such as:

>>> a=[1,2,3,4,5,6,7]
>>> A[len (a)-1]
>>> A[-len (a)]
>>> A[len (a)]
    
Traceback (most recent call last):
  File "<pyshell#98>", line 1, in <module>
    A[len (a)]
Indexerror:list index out of range
>>& Gt A[-len (a)-1]
    
Traceback (most recent call last):
  File "<pyshell#99>", line 1, <module>
    a[- Len (a)-1]
Indexerror:list index out of range

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.