[:-3: -1]Results in
- I really wanted to get
- If i try various things,
[-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")
How is the starting point of a step determined?
via Python 3 Cheat Sheet
- 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.
Thanks for your answer!
(The cheat sheet mystery will be put on hold for the time being ^^)
Answer # 1
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.
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.
Check the operation
>>>a = [1,2,3,4,5,6,7,8,9,10] >>>a [6: 3: -1] [7, 6, 5] >>>a [3: 6: -1] 
. 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  → 7
n = 1 → i + n * k = 5 → a  → 6
n = 2 → i + n * k = 4 → a  → 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
Operation Result Annotation s [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:The slice from i to j in
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.
4.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 areIf a negative value is given to
start and end, it is mechanically converted to
len (s) + ior
len (s) + j.
step behavior is index
x = 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,Is it three of
len if negative (s))
[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.
[10, 9]is obtained
- python - about "could not retrieve index file" error
- call function dynamically in python
- http error 503: service unavailable when doing amazon scraping with python
- python - cannot output the created sound data
- python even number sorts odd numbers
- python - [natural language processing] regarding back propagation of embedding layer when word2vec is implemented
- python - modelpredict() cannot be executed in tensorflow
- python csv even odd sorting method
- python - i want to change optional arguments according to conditions
- i want to replace every other character using python regular expression