Python advanced features-Slice ),

Source: Internet
Author: User

Python advanced features-Slice ),

Define a list:

1 = ['haha','xixi','hehe','heihei','gaga']

Take the first three elements:

>>> L[0],L[1],L[2]('haha', 'xixi', 'hehe')

This method is a bit silly, because if there are many elements, we need to take the first N of them. What should we do?

You may think of loop:

>>> r=[]>>> n = 3>>> for i in range(n):...     r.append(L[i])...>>> r['haha', 'xixi', 'hehe']

However, similar to this frequently-used operation method, almost all languages provide simple operation methods, similar to the Substring method (commonly known as getting substrings ), python also provides a similar method, which is Slice ).

For example:

>>> L[0:3]['haha', 'xixi', 'hehe']

Here, L [0: 3] indicates that index 3 is known from index 0, but does not include index 3, that is, index 0, 1, 2.

If the first index is 0, it can also be omitted:

>>> L[:3]['haha', 'xixi', 'hehe']

You can also start with any index:

>>> L[1:2]['xixi']

You can also try:

>>> L[1:1][]

Because Python also supports the reciprocal number L [-1], let's see if it supports reciprocal slice: (remember, the first last index is-1)

>>> L[-2:]['heihei', 'gaga']
>>> L[-3:-2]['hehe']

If you are not satisfied, continue with the following:

>>> M = list (range (100) # create a series from 0 to 99 using the range Function to form a list value assigned to m >>> m [0, 1, 2, 3, 4, 5, 6 ,......, 99]
>>> M [: 10] # obtain the top 10 numbers [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> m [-10:] # obtain the last ten numbers [90, 91, 92, 93, 94, 95, 96, 97, 98, 99] >>> m [10: 20] # obtain the first 11-20 numbers [10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> m [: 10: 2] # in the top 10, take one [0, 2, 4, 6, 8] >>> m [] # In the number 6-15, one [5, 8, 11, 14] for each 3] >>> m [:: 10] # In all the numbers, take one [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] for each 10 numbers >>> m [:] # Do not write anything. You can copy a list [0, 1, 2, 3, 4, 5, 6, 7,…, as is ,......, 99]

Tuple also supports the slicing feature, but the result is also a tuple:

>>> n = (1,3,5,7)>>> n[:3](1, 3, 5)

Let's take a look at the string:

>>> 'abcdefghjklmn'[::2]'acegjln'

The string also supports slicing, but the result is also a string.

Let's look at another example:

Using the slicing function, write a function trim (str), similar to the strip () function in Python -- remove spaces at the beginning and end of a string:

>>> def trim(str):...     while str[:1]==' ':...             str = str[1:]...     while str[-1:] == ' ':...             str = str[:-2]...     return str...>>> trim('  abc  hh  welcome!      ')'abc  hh  welcome!'

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.