Home>

### python - how the slicing step determines the starting point

• Slice`[1,2,3,4,5,6,7,8,9,10]`with`[:-3: -1]`Results in`[10,9]`
• I really wanted to get`[10,9,8]`
• If i try various things,`[:-4: -1]`and`[-1: -4: -1]`return the same value I understand
• However, in comparison with the figure below, the notation`[-1: -4: -1]`seems to cause the initial value and end value pairs to be shifted to the left.
(It seems that`[9,8,7]`seems to be obtained if -1 is taken in negative slice from "-1" to "-4")

via Python 3 Cheat Sheet

How is the starting point of a step determined?
• If i rely on cheat sheets,`[-0: -3: -1]`gives you`[10,9,8]`Will return an empty list (same as`[0: -3: -1]`)
• What is thespecificationof this strange behavior?
( The documentation has a bad view, Did not)
• Also, if you think "Slice with index value and discard end value", you can understand it consistently with other actions, but the cheat sheet as shown above Is why was produced? (Whether there was any intention or not expected, was there a separate headquarters, or just followed it?)

The reason why we are focusing on`reverse ()`with slices is
Because it was introduced in the following page that the execution speed of the slice is fast

I'd be happy if you have an objection/supplementary

• [python] Reverse with slices! !
• SO post featured above: Best way to create a "reversed" list in Python?

Thank you hayataka2049 and quiqui for answers.
I see, there was a rule`0<= n<(j-i)/k`. It was a blind spot that it was written in the built-in type!
This time, I would like to make quiqui who wrote down more easily the best answer.

(The cheat sheet mystery will be put on hold for the time being ^^)

Annotation 5.

s's "slice with i to j and step k" has index x = i + n * k (where n is an integer satisfying 0<= n<(ji)/k) It is defined as a sequence of elements.
>(omitted)
If k is negative, if i or j is greater than len (s)-1, use len (s)-1 instead. If i or j is omitted or None is specified, the value of"end"(which end depends on the sign of k) is used instead. Note that k cannot be zero.

and annotation 3

Note that -0 is still 0.

.

First,

``````>>>a = [1,2,3,4,5,6,7,8,9,10]
>>>a [6: 3: -1]
[7, 6, 5]
>>>a [3: 6: -1]
[]``````
Check the operation

. The story is then.

`a [6: 3: -1]`is i = 6 j = 3 k = -1 so 0<= n<(3-6)/(-1) 0<= n<3

n = 0 → i + n * k = 6 → a [6] → 7
n = 1 → i + n * k = 5 → a [5] → 6
n = 2 → i + n * k = 4 → a [4] → 5

.

`a [3: 6: -1]`is i = 3 j = 6 k = -1, so 0<= n<(3-6)/(-1) It means move in a range, but there is no n that satisfies 0<= n<-3, so it is an empty list.

`a [-0: -3: -1]`is equal to`a [0: 7: -1]`, 0<= n<(0 -7)/(-1) In other words, 0<= n<

The expected behavior is`a [:-4: -1]`(or you don't usually write`a [None: -4: -1]`).

`a [:-4: -1]`has a negative k value, so theendon the i side is the last and`a [9: -4 : -1]`and j is negative, so`a [9: 10-4: -1]`to`a [9: 6: -1].`

``` i = 9 j = 6 k = -1 so it moves to satisfy 0<= n<(6-9)/(-1). ,[a [9 + 0 * (-1)], a [9 + 1 * (-1)], a [9 + 2 * (-1)]]or[ 10, 9, 8]. ```
``` ```
• ``` Answer # 2 4. Built-in types — Python 3.6.5 documentation | 4.6.1. Common sequence operations According to                                                                           OperationResultAnnotations [i]s i-th element counting from 0(3)s [i: j]slices i through j(3) (4)s [i: j: k]s slices i through j, every k(3) (5)   Note:   3. If i or j is negative, the index is relative to the end of the sequence: len (s) + i or len (s) + j is used instead. Note that -0 is still 0.   The slice from i to j in4.s is defined as a sequence of elements with index k such that i<= k<j. If i or j is greater than len (s), use len (s). If i is omitted or None, 0 is used. If j is omitted or None, use len (s). If i is greater than or equal to j, the slice is an empty sequence.   5.s "slices i through j with step k" means any integer satisfying the index x = i + n * k, where n is 0<= n<(ji)/k ) Is defined as a sequence of elements with In other words, the index follows i, i + k, i + 2 * k, i + 3 * k, and stops when j is reached (but not including j). If k is a positive number, if i or j is greater than len (s), use len (s) instead. If k is negative, if i or j is greater than len (s)-1, use len (s)-1 instead. If i or j is omitted or None is specified, the value of"end"(which end depends on the sign of k) is used instead. Note that k cannot be zero. If None is specified for k, 1 is assumed to be specified. In summary, the rules involved this time are If a negative value is given tostart and end, it is mechanically converted tolen (s) + iorlen (s) + j. step behavior is indexx = i + n * k (n = 0,1,2,3, ..., 0<= n<(ji)/k) If i or j is omitted or None is specified, the value of"end"(which end depends on the sign of k) is used instead (0 if k is positive,len if negative (s)) Is it three of? Think about[1,2,3,4,5,6,7,8,9,10] [:-3: -1] If the omission is corrected and the negative value is converted,[1,2,3,4,5,6,7,8,9,10] [10: 7: -1]is obtained.n<3fromn<(j-i)/k. So[10, 9]is obtained ```
``` ```
``` ```
``` Related articlesinterpolation of unobserved point data with pythonpython list floating point in descending orderpython 3x - a program that determines whether or not the same character string can be created by joining any of the four input cpython 3x - cx_freeze error non-utf-8 code starting with'\x90' ```
``` ```
``` Related questions python - about "could not retrieve index file" errorcall function dynamically in pythonhttp error 503: service unavailable when doing amazon scraping with pythonpython - cannot output the created sound datapython even number sorts odd numberspython - [natural language processing] regarding back propagation of embedding layer when word2vec is implementedpython - modelpredict() cannot be executed in tensorflowpython csv even odd sorting methodpython - i want to change optional arguments according to conditionsi want to replace every other character using python regular expression ```
``` ```
``` (function() { if((".artleft pre").length > 0){ \$(".artleft pre").addClass('prettyprint linenums'); prettyPrint(); }; \$(".artleft p > code").each(function() { if (\$(this).text().length >= 30) { \$(this).addClass('linecode'); }else{} }); })(); ```
``` ```
``` © 2019 Programming tutorial for beginners ```