This article takes the example form to narrate the Python slice operation the usage, shares for everybody to refer for the reference, concretely as follows:
It is a very common operation to take part of a list or tuple element. For example, a list is as follows:
>>> L = [' Michael ', ' Sarah ', ' Tracy ', ' Bob ', ' Jack ']
Take the first 3 elements, what should be done?
The more stupid approach is as follows:
>>> [l[0], l[1], l[2]]
[' Michael ', ' Sarah ', ' Tracy ']
The reason is stupid way is because of expansion, take the first n elements can not.
Take the first N elements, which are indexed as 0-(N-1) elements, you can use loops:
>>> r = []
>>> n = 3
>>> for I in Range (n):
... R.append (L[i])
...
>>> r
[' Michael ', ' Sarah ', ' Tracy ']
For this type of operation that often takes a specified index range, the loop is tedious, so Python provides a slice (Slice) operator that simplifies the operation greatly .
Corresponding to the above problem, take the first 3 elements, with one line of code to complete the slice:
>>> L[0:3]
[' Michael ', ' Sarah ', ' Tracy ']
L[0:3] Indicates that it starts at index 0 until index 3, but does not include index 3. The index 0,1,2, which is exactly 3 elements.
If the first index is 0, you can also omit:
>>> L[:3]
[' Michael ', ' Sarah ', ' Tracy ']
You can also start with index 1 and take out 2 elements:
>>> L[1:3]
[' Sarah ', ' Tracy ']
Similarly, since Python supports l[-1] to take the penultimate element, it also supports reciprocal slices, try:
>>> l[-2:]
[' Bob ', ' Jack ']
>>> l[-2:-1]
[' Bob ']
Remember that the index of the last element of the countdown is-1.
Slicing is useful. Let's start by creating a 0-99 series:
>>> L = Range (m)
>>> L
[0, 1, 2, 3, ..., 99]
You can easily remove a segment of a series by slicing it. Like the first 10 numbers:
>>> L[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After 10 numbers:
>>> l[-10:]
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
Top 11-20 Number:
>>> l[10:20]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
First 10 number, each two takes one:
>>> L[:10:2]
[0, 2, 4, 6, 8]
All numbers, every 5 to one:
>>> L[::5]
[0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
Even write nothing, write only [:] You can copy a list as is:
>>> l[:]
[0, 1, 2, 3, ..., 99]
Tuple is also a list, the only difference is that tuple is immutable. Therefore, tuple can also be sliced, but the result of the operation is still tuple:
>>> (0, 1, 2, 3, 4, 5) [: 3]
(0, 1, 2)
String ' xxx ' or unicode string u ' xxx ' can also be viewed as a list, each element being a character. Therefore, strings can also be sliced, but the result of the operation is still a string:
>>> ' ABCDEFG ' [: 3]
' ABC '
>>> ' ABCDEFG ' [:: 2]
' ACEG '
In many programming languages, many kinds of intercepting functions are provided for strings, but the goal is to slice the string. Python does not have an intercept function for a string, it is simple to slice one operation.
Summarize:
With slicing operations, many local loops are no longer needed. python slices are very flexible, and a single line of code can accomplish a lot of line loops .
It is hoped that the examples described in this article will help you learn more about Python programming.