Home  >  Article  >  Backend Development  >  About the subscript problem of Sequence slicing and its solution

About the subscript problem of Sequence slicing and its solution

零下一度
零下一度Original
2017-06-17 11:00:021109browse

This article mainly introduces to you the relevant information about the Sequence slice subscript problem in Python. The article introduces it in detail through the example code, which has certain reference and learning value for everyone. Friends who need it Let’s take a look together below.

Preface

In python, slicing is a frequently used syntax, whether it is a tuple, a list or String, the general syntax is:

sequence[ilow:ihigh:step] # ihigh, step can be empty; for the sake of simplicity and ease of understanding, the usage of step is temporarily excluded Consider

Let’s briefly demonstrate the usage


sequence = [1,2,3,4,5]
sequence [ilow:ihigh] # 从ilow开始到ihigh-1结束
sequence [ilow:]  # 从ilow开始直到末尾
sequence [:ihigh]  # 从头部开始直到ihigh结束
sequence [:]   # 复制整个列表

The syntax is very concise and easy to understand. This syntax It is simple and easy to use in our daily use, but I believe that when we use this slicing syntax, we will habitually follow some rules:

  • ilow, ihigh are both smaller than sequence. Length

  • ilow a74f3b032e41184637d861f050ab599dsq_slice(s, i1, i2)

    , but this sq_slice is a bit special, because different objects have different corresponding functions. The following are the corresponding functions:

    // 字符串对象
    StringObject.c: (ssizessizeargfunc)string_slice, /*sq_slice*/
    
    // 列表对象
    ListObject.c: (ssizessizeargfunc)list_slice,  /* sq_slice */
    
    // 元组
    TupleObject.c: (ssizessizeargfunc)tupleslice,  /* sq_slice */

    Because the function implementations of the three of them are roughly the same, we only need to analyze one of them. The following is the analysis of the slicing function of the list:

    /* 取自ListObject.c */
    static PyObject *
    list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
    {
     PyListObject *np;
     PyObject **src, **dest;
     Py_ssize_t i, len;
     if (ilow < 0)
      ilow = 0;
     else if (ilow > Py_SIZE(a))    // 如果ilow大于a长度, 那么重新赋值为a的长度
      ilow = Py_SIZE(a);
     if (ihigh < ilow)  
      ihigh = ilow;
     else if (ihigh > Py_SIZE(a))    // 如果ihigh大于a长度, 那么重新赋值为a的长度 
      ihigh = Py_SIZE(a);
     len = ihigh - ilow;
     np = (PyListObject *) PyList_New(len); // 创建一个ihigh - ilow的新列表对象
     if (np == NULL)
      return NULL;
    
     src = a->ob_item + ilow;
     dest = np->ob_item;
     for (i = 0; i < len; i++) {    // 将a处于该范围内的成员, 添加到新列表对象
      PyObject *v = src[i];
      Py_INCREF(v);
      dest[i] = v;
     }
     return (PyObject *)np;
    }

    in conclusion

    As can be seen from the slicing function corresponding to the sq_slice function above, if when using slicing, the left and right subscripts are greater than the length of the sequence, they will be reassigned to the length of the sequence, so our initial slicing: print a[10:20] , what actually runs is: print a4:4 . Through this analysis, in the future when you encounter a slice with a subscript greater than the object length, you should I won’t be confused anymore~

The above is the detailed content of About the subscript problem of Sequence slicing and its solution. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn