Home>
  • 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 onreverse ()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 rule0<= 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

    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 toa [0: 7: -1], 0<= n<(0 -7)/(-1) In other words, 0<= n<

    The expected behavior isa [:-4: -1](or you don't usually writea [None: -4: -1]).

    a [:-4: -1]has a negative k value, so theendon the i side is the last anda [9: -4 : -1]and j is negative, soa [9: 10-4: -1]toa [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

                                                                              
    OperationResultAnnotation
    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:
      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 in

    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 are

    If a negative value is given to

    start 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