Python Slicing Usage Example tutorial

Source: Internet
Author: User
This article describes the usage of slicing operations in Python in the form of examples, and shares them for reference, as follows:

Taking a list or tuple element is a very common operation. For example, a list is as follows:

>>> L = [' Michael ', ' Sarah ', ' Tracy ', ' Bob ', ' Jack ']

What should I do if I take the first 3 elements?

The more stupid way is as follows:

>>> [l[0], l[1], l[2]][' Michael ', ' Sarah ', ' Tracy ']

The reason why is stupid is because of the extension, take the first n elements will be out of the way.

Take the first N elements, which are the elements indexed as 0-(N-1), and you can use loops:

>>> r = []>>> n = 3>>> for I in Range (n):   ... R.append (L[i]) ... >>> r[' Michael ', ' Sarah ', ' Tracy ']

It is cumbersome to use loops for operations that often take a specified index range, so Python provides a slice (Slice) operator that can greatly simplify this operation .

Corresponding to the above problem, take the first 3 elements, a line of code to complete the slice:

>>> l[0:3][' Michael ', ' Sarah ', ' Tracy '

L[0:3] Indicates that the fetch starts at index 0 until index 3, but does not include index 3. That is, 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 first element of the countdown, it also supports the inverse slice, try it:

>>> l[-2:][' Bob ', ' Jack ']>>> l[-2:-1][' Bob '

Remember that the index of the last element of the countdown is-1.

Slicing operations are useful. Let's start by creating a 0-99 series:

>>> L = range (+) >>> l[0, 1, 2, 3, ..., 99]

You can easily remove a segment of a sequence by slicing it. such as the first 10 numbers:

>>> l[:10][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

10 Number of second:

>>> l[-10:][90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Number of first 11-20:

>>> l[10:20][10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

First 10 numbers, each of two fetch one:

>>> l[:10:2][0, 2, 4, 6, 8]

All numbers, fetch one per 5:

>>> l[::5][0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]

Don't even write anything, just write [:] to copy a list as it is:

>>> l[:][0, 1, 2, 3, ..., 99]

Tuple is also a list, the only difference is that the tuple is immutable. Therefore, a tuple can also be used for slicing operations, but the result of the operation is still a tuple:

>>> (0, 1, 2, 3, 4, 5) [: 3] (0, 1, 2)

The string ' xxx ' or the unicode string u ' xxx ' can also be viewed as a list, each element being a character. Therefore, a string can also be manipulated with a slice, but the result of the operation is still a string:

>>> ' ABCDEFG ' [: 3] ' ABC ' >>> ' ABCDEFG ' [:: 2] ' ACEG '

In many programming languages, there are many kinds of interception functions available for strings, in fact, the purpose is to slice the strings. Python does not have an intercept function for a string, it can be done simply by slicing one operation.

Summarize:

With the slicing operation, many local loops are no longer needed. Python's slices are very flexible, and one line of code can implement many lines of work to complete .

It is hoped that the examples mentioned in this paper will help you to master Python program design.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.