Slicing operations in Python

Source: Internet
Author: User

The slicing operation in Python is very powerful, usually we use slices to extract information, to do related operations, here are some examples of slices.

Column as we take a multiple of 7 from the range function 1-100, the function and the result are as follows:

 for  in range (1,100) [6::7]:        print  i    7 14 21 28 35 42 49 56 63 70 77 84 91 98

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?

Stupid way:

>>> [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 = [] for in range (     N): ... >>> 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 the L[-1] first-to-last element, it also supports the inverse slice and tries:

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

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

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

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

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

>>> l[:101, 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:22, 4, 6, 8]

All numbers, fetch one per 5:

>>> l[::55, 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:

>>>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) [: 31, 2)

A string ‘xxx‘ or a Unicode string u‘xxx‘ can also be considered a list, with 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 '

There is also a slice that takes slices like a two-dimensional array in C, but is more advanced than a two-dimensional array in C, as shown in the following example:

 for  in range (1,100) [2::3][-10:]:                 print72 75 78 81 84 87 90 93 96 99            

is to use the range function to generate 1-100 of the number, then we take a multiple of 3, and go to the last 10.

The following is also a multiple of 3, but the first 10.

 for  in range (1,100) [2::3][:10]:          print  i    3 6 9  18 21 24 27 30  

In the example above, we see the power of slicing in python, and we hope to use it more later.

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.

Summary

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.

Slicing operations in Python

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.