This article example describes Python slicing operations. Shared for your reference, as follows:
We basically know that Python's sequence objects are elements that can be referenced by their index numbers, which can be either positive numbers starting from 0 and going from left to right, or negative numbers starting from -1 and going from right to left.
In Python, you can use the slice operation on any data that has a sequence structure. It should be noted that a sequence object returns an element at a certain index position, while a slice operation returns a copy of the same type of object as the object being sliced.
As in the following example, although both are an element, the object types are completely different:
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> alist[0] 0 >>> alist[0:1] [0]
Typically a slice operation is supplied with three parameters[start_index: stop_index: step]
start_indexis the starting position of the slice
stop_indexis the end position of the slice (not included)
stepCan not be provided, the default value is 1, the step value can not be 0, otherwise it will report an error ValueError.
(coll.) fail (a student)step is a positive number in terms oflist[start_index]
Starting at the element position, thesteppacelist[stop_index]
element positions (not included) up to and including the left-to-right interception of the
start_indexcap (a poem)stop_indexEither positive or negative indexing or a mix is fine, but make sure that thelist[stop_index]
The [logical] position of the element
must be inlist[start_index]
The element is to the right of the [logical] position of the element, otherwise the element cannot be fetched.
For example, several of the examples below are legitimate:
>>> 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]
(coll.) fail (a student)step When it is a negative number, the value of thelist[start_index]
Starting at the element position, thesteppacelist[stop_index]
element positions (not included) up to and including the right-to-left interception of the
start_indexcap (a poem)stop_indexEither positive or negative indexing or a mix is fine, but make sure that thelist[stop_index]
The [logical] position of the element
must be inlist[start_index]
Left of the [logical] position of the element, otherwise the element cannot be fetched.
For example, several of the examples below are legitimate:
>>> 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]
Suppose the length of the list (number of elements) islength, start_indexcap (a poem)stop_indexIn conformity with the logical positional relations of the virtual
start_indexcap (a poem)stop_indexThe absolute value of can be greater thanlengthThe. For example, 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]
additionallystart_indexcap (a poem)stop_indexAll of them can be omitted, such as in this formalist[:]
, omitted defaults to intercepting from the starting element of its corresponding left or right border.
Look at 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 for Slicing Operations in Python
(Note: Python methods (functions) with double-underscored names are called special methods, or magic methods, which are borrowed from Ruby.
Often special methods are the ones that should be called by the interpreter, and interfacing to the programmer is usually the cleaner looking way to go, such as the commonlen(list)
The essence is that the interpreter callslist.__len__()
methodology.)
In fact, in Python, both the list reference element and the elegantly simple slicing operation are called by the interpreter.list.__getitem__(x)
Special Methods.
>>> help(list.__getitem__) Help on method_descriptor: __getitem__(...) x.__getitem__(y) <==> x[y]
where x can be an integer object or a slice object.
alist[5]
cap (a poem)alist.__getitem__(5)
is completely equivalent.
>>> alist[5] 5 >>> alist.__getitem__(5) 5 >>>
The slice operation is called with 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 (. a[0:10:2]).
See example below.
>>> alist[1:7:2] [1, 3, 5] >>> slice_obj = slice(1,7,2) >>> alist.__getitem__(slice_obj) [1, 3, 5] >>>
Some common slicing operations
>>> alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Take the first part >>> alist[:5] [0, 1, 2, 3, 4] # Take the latter part >>> alist[-5:] [5, 6, 7, 8, 9] # Fetch elements in even positions >>> alist[::2] [0, 2, 4, 6, 8] # Take the odd position element >>> alist[1::2] [1, 3, 5, 7, 9] # Shallow replication, equivalent to () more object-oriented writing >>> blist = alist[:] >>> blist [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Return a reverse-order list, recommended reversed(list) write, more intuitive to understand. >>> alist[::-1] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] # Insert multiple elements at a given position >>> 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]
From the above examples you can see Python's slicing operation is very flexible, powerful, simple, elegant, if you can fully grasp and correctly use the level of your Python code will be greatly enhanced.
Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.