Home  >  Article  >  Backend Development  >  Introducing the methods and examples of dealing with too long slice parameters in python

Introducing the methods and examples of dealing with too long slice parameters in python

coldplay.xixi
coldplay.xixiforward
2020-12-15 17:05:203125browse

python tutorialThe column introduces how to deal with slice parameters that are too long

Introducing the methods and examples of dealing with too long slice parameters in python

Many friends’ understanding of the concept of slice parameters remains conceptual, slicing There are three parameters, namely step, start and stop. Because the values ​​of parameters are also variable, we need to process them in the next step. In the previous explanation of slice, we mentioned the problem of too long list data, and this problem also exists in parameters. Next, we will explain the respective processing of the three parameters of step, start, and stop to help you have an in-depth understanding of the parameter issues in slice.

Related free learning recommendations: python tutorial (video)

1.step processing

if (r->step == Py_None) {
     /* step 默认是 1,这不难理解 */
   *step = 1;
 } else {
   if (!_PyEval_SliceIndex(r->step, step)) return -1;
     /* step 不能为零,否则报 ValueError,要注意的是,这个异常是在执行 BINARY_SUBSCR 才报出来,
    * 在创建 slice 对象时如果 step 为 0 并不会报错 */
   if (*step == 0) {
     PyErr_SetString(PyExc_ValueError, "slice step cannot be zero");
     return -1;
   }
   /* step 的最小值,他是根据 size_t 来定义的
    * #define PY_SSIZE_T_MAX ((Py_ssize_t)(((size_t)-1)>>1))
    * 所以在 32 为系统上就是 -2147483647 */
   if (*step < -PY_SSIZE_T_MAX)
     *step = -PY_SSIZE_T_MAX;
 }

2.start processing

/* 当 start 未设置时的默认值,length 是序列的长度
  * 如果切片从序列头部开始(step > 0),start = 0
  * 如果切片从序列末尾开始(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;
   /* 如果 start 是负数,其实是通过加上序列长度转化成正数的 a[-1:] <=> a[4:] */
   if (*start < 0) *start += length;
   /* 如果加上 length 还是小于 0,也就是 -start 超出了序列长度,这时候会根据 step 的正负将start
    * 设置为序列的开始(0)或结束(-1)位置
    * a[-6:-1]  <=> a[0:-1]
    * a[-6:-1:-1] <=> a[-1:-1:-1] */
   if (*start < 0) *start = (*step < 0) ? -1 : 0;
    /* start 超出了序列长度,这时候会根据 step 的正负将start
    * 设置为序列的长度或序列长度减 1(最后一个元素)
    * a[6:-1]  <=> a[5:-1]
    * a[6:-1:-1] <=> a[4:-1:-1] */
   if (*start >= length)
     *start = (*step < 0) ? length - 1 : length;
 }

3.stop processing

/* 当 stop 未设置时的默认值,length 是序列的长度
  * 如果切片从序列头部开始(step > 0),stop = length,比最后一个元素的下标多 1
  * 如果切片从序列末尾开始(step < 0),start = -1,比第一个元素的下标少 1 */
 defstop = *step < 0 ? -1 : length;
 if (r->stop == Py_None) {
   *stop = defstop;
 } else {
   if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
   /* 如果 stop 是负数,其实是通过加上序列长度转化成正数的 a[:-1] <=> a[:4] */
   if (*stop < 0) *stop += length;
   /* 如果加上 length 还是小于 0,也就是 -stop 超出了序列长度,这时候会根据 step 的正负将 stop
    * 设置为序列的开始(0)或结束(-1)位置
    * 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
  • Start from the beginning, start The index number can be omitted, but the colon cannot be omitted.
  • ends at the end. The ending index number can be omitted, but the colon cannot be omitted.
  • The step size defaults to 1. If sliced ​​continuously, both numbers and colons can be omitted.

About the expansion of slice operation in Python:

The complete syntax of slice operation in Python:

# i默认是0
# j默认是len(S)
# k的步长,默认为+1
S[i:j:k]

where i, j, k can all be negative numbers:

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

If k < 0, it means [i,k ) between the characters according to the step size k, counting from right to left instead of from left to right

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

>>>S[-1:-3:-1]  # 将位于S[-1:-3]的字符子串,按照步长1,从右往左数,而不是从左往右数
'gf'

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

>>>S[2:4:-1]  # 输出空字符串
''

>>>S[::-1]  # 逆序
'gfedcba'

It should be pointed out that the form of s[i:j:k] is equivalent to the following Format:

>>>S = 'abcdefg'
>>>S[slice(None, None, -1)]  # 等价于使用slice对象进行数组元素的访问操作
'gfedcba'

This concludes this article about the processing methods and examples of excessively long slice parameters in python.

The above is the detailed content of Introducing the methods and examples of dealing with too long slice parameters in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete