Treatment method and example of slice parameter excessively long in python

  • 2021-08-28 20:25:18
  • OfStack

Many small partners' conceptual understanding of slice parameters stays on the concept, and there are three sliced parameters, namely step, start and stop. Because the values of parameters are also changeable, we need to process them in the next step. In the previous slice explanation, we mentioned the problem that the list data is too long, and there is also such a problem in the parameters. Below, we will explain the separate processing of step, start and stop, so as to help you deeply understand the parameter problems in slice.

1. Treatment of step


if (r->step == Py_None) {
     /* step  The default is  1 This is not difficult to understand  */
   *step = 1;
 } else {
   if (!_PyEval_SliceIndex(r->step, step)) return -1;
     /* step  Can't be zero, otherwise report  ValueError Note that this exception is executed during the  BINARY_SUBSCR  Just reported it, 
    *  In creating  slice  Object if  step  For  0  Will not report an error  */
   if (*step == 0) {
     PyErr_SetString(PyExc_ValueError, "slice step cannot be zero");
     return -1;
   }
   /* step  He is based on the minimum value of  size_t  To define 
    * #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
    *  So in  32  For the system is  -2147483647 */
   if (*step < -PY_SSIZE_T_MAX)
     *step = -PY_SSIZE_T_MAX;
 }

2. Treatment of start


/*  When  start  The default value when not set, length  Is the length of the sequence 
  *  If the slice starts at the head of the sequence ( step > 0 ), start = 0
  *  If the slice starts at the end of the sequence ( step < 0 ), start = length - 1 */
 defstart = *step < 0 ? length-1 : 0;
 if (r->start == Py_None) {
   *start = defstart;
 }
 else {
   if (!_PyEval_SliceIndex(r->start, start)) return -1;
   /*  If  start  Is a negative number, which is actually converted into a positive number by adding the sequence length  a[-1:] <=> a[4:] */
   if (*start < 0) *start += length;
   /*  If you add  length  Or less than  0 That is,  -start  Exceeds the sequence length, which will be based on the  step  The positive and negative of will start
    *  To the start of the sequence ( 0 ) or end ( -1 ) Position 
    * a[-6:-1]  <=> a[0:-1]
    * a[-6:-1:-1] <=> a[-1:-1:-1] */
   if (*start < 0) *start = (*step < 0) ? -1 : 0;
    /* start  Exceeds the sequence length, which will be based on the  step  The positive and negative of will start
    *  To the length of the sequence or the length of the sequence minus  1 (Finally 1 Elements) 
    * a[6:-1]  <=> a[5:-1]
    * a[6:-1:-1] <=> a[4:-1:-1] */
   if (*start >= length)
     *start = (*step < 0) ? length - 1 : length;
 }

3. Treatment of stop


/*  When  stop  The default value when not set, length  Is the length of the sequence 
  *  If the slice starts at the head of the sequence ( step > 0 ), stop = length , more than the last 1 There are many subscripts for each element  1
  *  If the slice starts at the end of the sequence ( step < 0 ), start = -1 , Betty 1 Fewer subscripts for 1 element  1 */
 defstop = *step < 0 ? -1 : length;
 if (r->stop == Py_None) {
   *stop = defstop;
 } else {
   if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
   /*  If  stop  Is a negative number, which is actually converted into a positive number by adding the sequence length  a[:-1] <=> a[:4] */
   if (*stop < 0) *stop += length;
   /*  If you add  length  Or less than  0 That is,  -stop  Exceeds the sequence length, which will be based on the  step  The positive and negative of will  stop
    *  To the start of the sequence ( 0 ) or end ( -1 ) Position 
    * a[3:-6]  <=> a[3:0]
    * a[3:-6:-1] <=> a[3::-1] */
   if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
   if (*stop >= length)
     *stop = (*step < 0) ? length - 1 : length;
 }

Note:

The specified interval is left open and right closed Starting from scratch, the starting index number can be omitted, but the colon cannot be omitted At the end of the end, the ending index number can be omitted, and the colon cannot be omitted. The step size defaults to 1. If you slice continuously, both numbers and colons can be omitted. About the slice operation extension in Python:

The complete syntax for the slice operation in Python:


# i The default is 0
# j The default is len(S)
# k The default is the step size of +1
S[i:j:k]

Where i, j, k can all be negative:

If i < 0 or k < 0, equivalent to len (S) + i, or len (S) + j;

If k < 0, it means that the characters between [i, k] are counted from right to left according to the step size k, instead of from left to right


>>>S = 'abcdefg'
>>>S[-3:-1]
'ef'

>>>S[-1:-3:-1]  #  Will be located in the S[-1:-3] Character substring of, in step size 1 , count from right to left, not from left to right 
'gf'

>>>S[4:2:-1]
'ed'

>>>S[2:4:-1]  #  Output an empty string 
''

>>>S[::-1]  #  Reverse order 
'gfedcba'

It should be noted that the form s [i: j: k] is equivalent to the following form:


>>>S = 'abcdefg'
>>>S[slice(None, None, -1)]  #  Equivalent to using slice Object to access array elements 
'gfedcba'

Related articles: