Python notes Anatomy of the Python Slice (slicing) syntax

Source: Internet
Author: User

It is also easy to understand the following slicing behavior, even for novice python:
>>> s = ' this_is_a_test ' >>> s[1:5] ' his_ '
Further, the following syntax and output are not difficult to understand:
>>> s = ' this_is_a_test ' >>> s[:: 2] ' ti_sats '
So, what about the following?
>>> s = ' this_is_a_test ' >>> s[::-1] ' tset_a_si_siht '  # # Why has s been reversed? >>> S[1:6:-1] '  # # Why just get an empty string? >>> s[6:1:-1]  ' Si_si '  # # Why is this result?
Do you think the result is a bit strange when the 3rd parameter of the slicing expression (step, "stride" or "step") is negative?
To be honest, it's normal for novices to be confused about the results (and, of course, if you fully understand the "why", then congratulations, because you should have a solid grasp of the Python grammar Basics).
This note is to dissect the syntax of the slicing expression and try to solve the puzzle of the above example for a Python beginner.

1. Slice expressions (slicing expression)
The Python official documentation explains the slicing syntax very concisely:
A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings May is used as expressions or as targets in assignment or DEL statements.
According to the documentation, the slice syntax is divided into two categories: simple slices (simple_slicing) and extended slices (extended_slicing), respectively, as described below.

2. Simple slicing (simple_slicing)
Simple slicing syntax is as follows:
S[begin:end]
where, s represents any object of type sequence, begin and end must be an int value or an expression that evaluates to an int value, and their semicolon together form a front-closed interval (that is, begin <= idx < end), A begin and end that indicates the index value of the source sequence s that the slice operation will access, where the true end position is the previous position of end in S, that is, the last element of the sequence returned by the slice is indexed in the source sequence s (end-1).
NOTE 1: begin and end can be defaulted, at which point their default values are 0 and sys.maxint
NOTE 2: The values of begin and end can exceed the actual length of the sequence, such as S = [1, 2, 3]; S[0:8] is also legal
NOTE 3: begin and end can all be negative values. According to the Convention implemented within the Python interpreter, 1 is the index of the last element of the sequence S, 2 is the index of the second-to-bottom element, and so on. We can understand the correspondence of positive/Negative index numbers (, The following is fine-tuned for the position of the indexed values in the original document, which the individual thinks is easier for most people to understand):

by When Python internally represents a sequence type, the valid index range is [0, Len (s)-1] If the positive index value is represented, and the valid index range is [-len (s),-1] If the negative index value is used.
If you can keep this in mind, some of the seemingly confusing grammatical behaviors about slice are easier to understand.
The following slicing expression is a simple slice syntax:
>>> s = [' A ', ' B ', ' C ', ' d ', ' e ', ' F ']>>> S[0:len (s)] # # output: [' A ', ' B ', ' C ', ' d ', ' e ', ' F '] >&G T;> s[0:-1]     # # output: [' A ', ' B ', ' C ', ' d ', ' E ']>>> s[:-1]      # # output: [' A ', ' B ', ' C ', ' d ', ' e '] &g T;>> s[2:-1]     # # output: [' C ', ' d ', ' E ']>>> s[-3:-1]    # # output: [' d ', ' E ']>>> s[-3:]< c4/>## output: [' d ', ' e ', ' F ']>>> s[3:1]      # # output: []

3. Extended slices (extended_slicing)
The extended slice syntax is as follows:
S[begin:end:stride]
In contrast to the simple slice syntax, the extended slice simply adds a 3rd parameter , the step parameter (commonly referred to as "stride" or "step" in the English material).
The "stride" parameter introduced by the extended slice syntax is a parameter that requires special attention, because its positive/negative values will affect the direction of the slice operation's access to the source sequence s , which is why the first few examples of this article may be confusing to novice python.
in fact, the rules are very simple, it is not worth a penny:
1) when the Stride parameter is positive (positive), it indicates that the slice operation accesses the element of the source sequence s from left to right (that is, forward) , at which point the begin and end parameters are defaulted. The Python interpreter is set to none by default. If s[0:: 1] will be interpreted as s[0:none:1], the end actually takes a value greater than the upper limit of its valid index range to ensure that the slice operation can access all the elements from the start of the source sequence s (left to right).
2) when the Stride parameter is negative (negative), it indicates that the slice operation accesses the element of the source sequence s from right to left (that is, reverse) , at which point the begin and end parameters are defaulted. The Python interpreter is set to none by default. such as s[-1::-1] will be interpreted as S[-1:none:-1], at this point, the actual value of end is less than the lower limit of its valid index range, has ensured that the tile operation can access to the source sequence s from begin all elements (reverse, right-to-left).
3) the start and end index values of the slice expression need to be guaranteed to be in the direction of access for the slice operation, regardless of whether the stride parameter is positive or negative, and there is an element between begin and end, so that the slice operation is guaranteed to return a non-empty set.
These 3 rules should be easily understood through Python's internal index value conventions for sequence objects given in section 2nd of this article.
here, the reason for the output of the example at the beginning of this article must be clear.

>>> s = ' this_is_a_test ' >>> s[::-1] ' tset_a_si_siht '  # # Q: Why has s been reversed? A: Step Parameter-1 indicates reverse access s>>> s[1:6:-1] '  # # # Q: Why just get an empty string? A: The step parameter is negative, reverse access, but begin and end no element, so return to empty string >>> s[6:1:-1]  ' Si_si '  # # Q: Why is this result? A: Reverse access, slice access to the element positive index range is [2:6], so return to ' Si_si '

remark: As described in section 6th, "Python core programming", this is called "extension" because the early Python interpreter only supports simple slices without step parameters, and the slice operation with step size is implemented in Python extension. 1.2. In fact, the current CPython interpreter has long supported slice operations with step parameters for pure python, but as a historical name, the word "extended" is preserved.

"References"
1. Python docs:slicings
2. Python Docs:an informal Introduction to Python-strings
3. Section 6.1.2 & 6.3.2 of <core Python programming>, that is, Python core programming book 6th. 1.2 and 6.3.2
4. StackOverflow:Explain Python ' s slice notation 

========================= EOF ======================



Python notes Anatomy of the Python Slice (slicing) syntax

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.