Python Advanced Features-slicing

Source: Internet
Author: User

In Python, the more code is better, the less the better. The more complex the code is, the better it is, but the simpler the better.

List Slice
>>> L = ['Michael','Sarah','Tracy','Bob','Jack']>>> L[0:3]['Michael','Sarah','Tracy']
#如果索引以0开始

>>> L[:3]
[' Michael ', ' Sarah ', ' Tracy ']

Inverted slices

>>> l[-2:]   #倒数几个就是负几 ['Bob'Jack ' ]>>> l[-2:-1['Bob']

Example:

1: Traverse out 1-100

>>> L = list (range (100))

2: Take out 1-10-bit, how many bits is l[:10]

>>> l[:101, 2, 3, 4, 5, 6, 7, 8, 9]

3:10 digits after removal

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

4: Remove 11-20-bit

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

5: First 10 numbers, each 5 take one

>>>l[:10:2]# back [2,4,6,8]

6: All numbers per 5 take one

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

7: Copy List

>>>1, 2, 3, ..., 99]

Tuple slices
>>> (1,2,3,4,5,6) [: 3] (1, 2, 3)

' xxx ' can also be seen as a list

' ABCDEFG ' [: 3] ' ABC ' ' ABCDEFG ' [:: 2] ' Aceg '

Python Advanced Features-slicing

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.