Python slice operation in detail

  • 2020-11-25 07:22:01
  • OfStack

An example of Python slicing is presented in this article. To share for your reference, the details are as follows:

We basically know that Python's sequence objects are elements that can be referenced with index Numbers, which can be positive Numbers starting at 0 and left to right, or negative Numbers starting at -1 and right to left.

Slicing can be used in Python for data with sequence structure. It should be noted that the index position of the sequence object returns 1 element, while the slicing operation returns a copy of the same type of object as the slicing object.

In the following example, the object type is completely different even though both are 1 element:


>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[0]
0
>>> alist[0:1]
[0]

Generally, 3 parameters are provided for a slice operation [start_index:  stop_index:  step]

start_index is the starting position of the slice
stop_index is the end of the slice (not included)
step can not be provided, the default value is 1, step value can not be 0, otherwise it will report an error ValueError.

When step is positive, so list[start_index] Element position starts with step as the step size to list[stop_index] Element position (not included), truncated from left to right,

start_index and stop_index can be used in either positive or negative indices or mixed, but be sure list[stop_index] The logical position of an element

Must be in list[start_index] Element to the right of the logical position, otherwise the element cannot be extracted.

For example, the following examples are all legal:


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

When step is negative, with list[start_index] Element position starts with step as step size up to list[stop_index] Element position (not included), cut from right to left,

start_index and stop_index can be used in either positive or negative indices or mixed, but be sure list[stop_index] The logical position of an element

Must be in list[start_index] Element to the left of the logical position, otherwise the element cannot be extracted.

For example, the following examples are all legal:


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

If the length (number of elements) of list is length, start_index and stop_index in accordance with the virtual logical position relationship,

The absolute value of start_index and stop_index can be greater than length. Take the following two examples:


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

In addition, start_index and stop_index can be omitted, such as this form alist[:] , the omitted default begins interception by its corresponding left and right boundary starting element.

Take a look at the following specific examples:


>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Implementation mechanism of slicing operation in Python

(Note: The method (function) in Python with double underlined names is called the special method, or the magic method, borrowed from ruby.

Special methods are usually called by the interpreter, and interfaces to the programmer are often more succinct, as is often the case list[start_index]0

The essence is the interpreter call list.__len__() Method).

In fact, the list reference element and the elegantly concise slicing operations in Python are all called by the interpreter list.__getitem__(x) Special methods.


>>> help(list.__getitem__)
Help on method_descriptor:
__getitem__(...)
  x.__getitem__(y) <==> x[y]

x can be an integer object or a slice object.

alist[5] and alist.__getitem__(5) It's completely equivalent.


>>> alist[5]
5
>>> alist.__getitem__(5)
5
>>>

The slice operation calls the slice object as a parameter __getitem__() .


>>> help(slice)
Help on class slice in module builtins:
class slice(object)
 | slice(stop)
 | slice(start, stop[, step])
 |
 | Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).

See the following example.


>>> alist[1:7:2]
[1, 3, 5]
>>> slice_obj = slice(1,7,2)
>>> alist.__getitem__(slice_obj)
[1, 3, 5]
>>>

1. Some common slicing operations


>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#  Take before 1 Part of the 
>>> alist[:5]
[0, 1, 2, 3, 4]
#  Take after 1 Part of the 
>>> alist[-5:]
[5, 6, 7, 8, 9]
#  Take an even position element 
>>> alist[::2]
[0, 2, 4, 6, 8]
#  Take an odd number of elements 
>>> alist[1::2]
[1, 3, 5, 7, 9]
#  Shallow copy is equivalent to list.copy() More object-oriented writing 
>>> blist = alist[:]
>>> blist
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#  return 1 An inverted list, recommended reversed(list) More intuitive to understand. 
>>> alist[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#  Insert multiple elements in one place 
>>> alist[3:3] = ['a','b','c']
>>> alist
[0, 1, 2, 'a', 'b', 'c', 3, 4, 5, 6, 7, 8, 9]
#  Insert multiple elements before the start position 
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[:0] = ['a','b','c']
>>> alist
['a', 'b', 'c', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#  Replace multiple elements 
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> alist[0:3] = ['a','b','c']
>>> alist
['a', 'b', 'c', 3, 4, 5, 6, 7, 8, 9]
#  Delete slices 
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> del alist[3:6]
>>> alist
[0, 1, 2, 6, 7, 8, 9]

As can be seen from the above examples, Python slicing operation is very flexible, powerful, concise, elegant, if you can fully grasp and correct use of Python code level will be greatly improved.

For more information about Python, please refer to Python Data Structure and Algorithm Tutorial, Python Function Technique Summary, Python String Manipulation Technique Summary, Python Introduction and Advanced Classic Tutorial and Python File and Directory Manipulation Technique Summary.

I hope this article has been helpful in Python programming.


Related articles: