Slicing a list

Source: Internet
Author: User

Taking a partial element of a list is a very common operation. For example, a list is as follows:

>>> L = [' Adam ', ' Lisa ', ' Bart ', ' Paul ']

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

Stupid way:

>>> [l[0], l[1], l[2]][' Adam ', ' Lisa ', ' Bart ']

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[' Adam ', ' Lisa ', ' Bart ']

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][' Adam ', ' Lisa ', ' Bart '

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][' Adam ', ' Lisa ', ' Bart '

You can also start with index 1 and take out 2 elements:

>>> l[1:3][' Lisa ', ' Bart ' 

Use only one : to indicate from beginning to end:

>>> l[:][' Adam ', ' Lisa ', ' Bart ', ' Paul '

So l[:] Actually copied out a new list.

The slice operation can also specify a third parameter:

>>> l[::2][' Adam ', ' Bart ']

The third parameter indicates that each n takes one, and the top L[::2] takes one out of every two elements, that is, one at a intervals.

Change the list to a tuple, the slice operation is exactly the same, but the result of the slice becomes a tuple.

The range () function can create a sequence number:

>>> Range (1, 101) [1, 2, 3, ..., 100]

Please use the slices to remove:

1. The first 10 digits;
2. Multiples of 3;
3. A multiple of 5 not greater than 50.

Reference code:

L = Range (1, 101)

Print L[0:10]
Print L[2::3]
Print L[4:50:5]

Print L[0:10] #从第1个数元素开始取, to the end of the 11th element
Print L[2::3] #从第三元素开始取, take one element at every 2
Print L[4:50:5] #从第五个取, take one every 4, ' start element ': ' last element ': ' Take element interval '

Reverse sectioning

For list, since Python supports l[-1] to take the first element of the countdown, it also supports the inverse slice, try it:

>>> L = [' Adam ', ' Lisa ', ' Bart ', ' Paul ']>>> l[-2:][' Bart ', ' Paul ']>>> l[:-2][' Adam ', ' Lisa ']& gt;>> l[-3:-1][' Lisa ', ' Bart ']>>> l[-4:-1:2][' Adam ', ' Bart '

Remember that the index of the first element of the countdown is-1. The reverse slice contains the starting index and does not contain the end index.

Task

Use the reverse section to remove the 1-100 sequence:

* last 10 digits;

* A multiple of the last 10 5.

Reference code:
L = Range (1, 101)
Print l[-10:]
Print l[4::5][-10:]//Get a multiple of 5 before taking 10
Slicing the string

The string ' xxx ' and 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 ' [-3:] ' EFG ' >>> ' 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.

Task

The string has a method upper () to turn the character into uppercase:

>>> ' abc '. UPPER () ' ABC '

But it will turn all the letters into uppercase. Design a function that takes a string and returns a string with only the first letter capitalized.

tip: use slice manipulation to simplify string manipulation.

Reference code:

def firstcharupper (s):
Return S[0].upper () +s[1:]

Print firstcharupper (' Hello ')
Print Firstcharupper (' Sunday ')
Print Firstcharupper (' September ')

Slicing a list

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.