# Understanding Slice Symbols

I need a good explanation of the Python slice symbol (the reference is a plus sign).

For me, this notation requires some attention.

It looks very powerful, but I don't fully understand it yet.

### #1st floor

In Python 2.7

Slice with Python

``````[a:b:c]

len = length of string, tuple or list

c -- default is +1. The sign of c indicates forward or backward, absolute value of c indicates steps. Default is forward with step size 1. Positive means forward, negative means backward.

a --  When c is positive or blank, default is 0. When c is negative, default is -1.

b --  When c is positive or blank, default is len. When c is negative, default is -(len+1).
``````

Understanding index allocation is important.

``````In forward direction, starts at 0 and ends at len-1

In backward direction, starts at -1 and ends at -len
``````

When you say [a:b:c], what you are saying is based on the symbol of C (forward or backward), starting with a and ending with B (excluding the element at the bth index).Using the index rules above, keep in mind that you will only find elements in this range:

``````-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1
``````

But this range can continue indefinitely in both directions:

``````...,-len -2 ,-len-1,-len, -len+1, -len+2, ..., 0, 1, 2,3,4 , len -1, len, len +1, len+2 , ....
``````

For example:

``````             0    1    2   3    4   5   6   7   8   9   10   11
a    s    t   r    i   n   g
-9  -8  -7   -6   -5  -4   -3  -2  -1
``````

If your choice of a, b, and c allows you to use the rules of a, b, and c above to iterate over the range above, you will get a list of elements (touched during the iteration), or you will get an empty list.

Last thing: If a and b are equal, you will also get an empty list:

``````>>> l1
[2, 3, 4]

>>> l1[:]
[2, 3, 4]

>>> l1[::-1] # a default is -1 , b default is -(len+1)
[4, 3, 2]

>>> l1[:-4:-1] # a default is -1
[4, 3, 2]

>>> l1[:-3:-1] # a default is -1
[4, 3]

>>> l1[::] # c default is +1, so a default is 0, b default is len
[2, 3, 4]

>>> l1[::-1] # c is -1 , so a default is -1 and b default is -(len+1)
[4, 3, 2]

>>> l1[-100:-200:-1] # Interesting
[]

>>> l1[-1:-200:-1] # Interesting
[4, 3, 2]

>>> l1[-1:-1:1]
[]

>>> l1[-1:5:1] # Interesting
[4]

>>> l1[1:-7:1]
[]

>>> l1[1:-7:-1] # Interesting
[3, 2]

>>> l1[:-2:-2] # a default is -1, stop(b) at -2 , step(c) by 2 in reverse direction
[4]
``````

### #2nd floor

``````Index:
------------>
0   1   2   3   4
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
0  -4  -3  -2  -1
<------------

Slice:
<---------------|
|--------------->
:   1   2   3   4   :
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
:  -4  -3  -2  -1   :
|--------------->
<---------------|
``````

Reference resources: http : //wiki.python.org/moin/MovingToPythonFromOtherLanguages

### #3rd floor

You can also use slice assignment to remove one or more elements from the list:

``````r = [1, 'blah', 9, 8, 2, 3, 4]
>>> r[1:4] = []
>>> r
[1, 2, 3, 4]
``````

### #4th floor

Python Slice Symbol:

``````a[start:end:step]
``````
• For start and end, negative values are interpreted as relative to the end of the sequence.
• The positive end index indicates the position after the last element to be included.
• The default values for blank values are as follows: [+0:-0:1].
• Using negative steps reverses the interpretation of start and end

The symbol extends to (numpy) matrices and multidimensional arrays.For example, to slice an entire column, you can use:

``````m[::,0:2:] ## slice the first two columns
``````

Slices contain references to array elements, not copies.If you want to create a separate copy of the array, you can use deepcopy() .

### #5th floor

Often, writing code with many hard-coded index values can lead to confusion in readability and maintenance.For example, if you return the code a year later, you'll look at it and want to know what you think when you write it.The solution shown is just a way to get a clearer picture of how the code actually works.Typically, a built-in slice () creates a slice object that can be used anywhere a slice is allowed.For example:

``````>>> items = [0, 1, 2, 3, 4, 5, 6]
>>> a = slice(2, 4)
>>> items[2:4]
[2, 3]
>>> items[a]
[2, 3]
>>> items[a] = [10,11]
>>> items
[0, 1, 10, 11, 4, 5, 6]
>>> del items[a]
>>> items
[0, 1, 4, 5, 6]
``````

If you have a slice instance s, you can get more information about it by looking at its s.start, s.stop, and s.step properties, respectively.For example:

``>>> a = slice(10, 50, 2) >>> a.start 10 >>> a.stop 50 >>> a.step 2 >>>``

Tags: Python

Posted on Tue, 03 Dec 2019 18:23:37 -0500 by ubuntu-user