I don't understand slicing with negative bounds in Python. How is this supposed to work?

后端 未结 7 534
臣服心动
臣服心动 2020-12-31 02:19

I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:

>         


        
相关标签:
7条回答
  • 2020-12-31 03:01
    >>> l = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz&']
    
    # I want a string up to 'def' from 'vwx', all in between
    # from 'vwx' so -2;to 'def' just before 'abc' so -9; backwards all so -1.
    >>> l[-2:-9:-1]
    ['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']
    
    # For the same 'vwx' 7 to 'def' just before 'abc' 0, backwards all -1
    >>> l[7:0:-1]
    ['vwx', 'stu', 'pqr', 'mno', 'jkl', 'ghi', 'def']
    

    Please do not become listless about list.

    1. Write the first element first. You can use positive or negative index for that. I am lazy so I use positive, one stroke less (below 7, or -3 for the start).
    2. Index of the element just before where you want to stop. Again, you can use positive or negative index for that (below 2 or -8 for stop).
    3. Here sign matters; of course - for backwards; value of stride you know. Stride is a 'vector' with both magnitude and direction (below -1, backwards all).

      l = [0,1,2,3,4,5,6,7,8,9]
      l[7:2:-1], l[-3:2:-1], [-3:-8:-1],l[7:-8:-1]
      

      All result in [7, 6, 5, 4, 3].

    0 讨论(0)
  • 2020-12-31 03:04

    If we want to print from the back-end of the string we can go for negative indexing. Indexing starts from -1.

    Example : s = 'hello world'

    s[-11:-1] = 'hello worl' s[-1:-11] = '' // beginning value should be lower(i.e., in this case -1 greater than -11) if it is greater it won't print anything.

    0 讨论(0)
  • 2020-12-31 03:07

    I'll be addressing a point that some others have missed:

    How do we interpret this negative index, in the context of what we know about slicing?

    Generally, when we do a slice, we talk about [inclusive, exclusive] bounds. So

    A = [1,3,4,6]
    A[1:3] # will give us 3-1 = 2 elements, index 1 and 2 => [3,4]
    

    So when we have a negative index in the slice, A[1:-1], this means we have A[1:len(A)-1] = A[1:3] which gives us again index 1 and 2 and hence [3,4].

    Note that even though the list has length 4, its last index is 3, hence why this -1 notation will work here. Also note that if you have code that takes in the negative index as a variable, then you'll need to manually check for 0, since A[:-0] == A[:0] == [] `

    0 讨论(0)
  • 2020-12-31 03:08

    The crucial point is that python indices should be thought of as pointers to the spaces between the entries in a list, rather than to the elements themselves. Hence, 0 points to the beginning, 1 to between the first and second, ... and n to between the nth and (n+1)st.

    Thus l[1:2] gives you a list containing just element l[1] since it gives you everything between the two pointers.

    Similarly, negative indices point in between elements, but this time counting from the back, so -1 points between the last element and the next-to-last, so [0:-1] refers to a block of items not including that last one.

    As syntactic sugar, you can leave off 0 from the beginning or, in effect, the end, so l[n:] refers to everything from l[n] to the end (if n>=len(l) then it returns the empty list).

    0 讨论(0)
  • 2020-12-31 03:08

    Yes, calling s[0:-1] is logically the same thing as s[:-1] since slicing is best defined as:

    [beginning_index:ending_index]
    

    Python allows you to omit 0 as this allows your code to more terse.

    0 讨论(0)
  • 2020-12-31 03:15

    Negative indices are counted from the end, so s[:-1] is equivalent to s[:len(s)-1] and s[-1] is the last element, for example.

    0 讨论(0)
提交回复
热议问题