Python slice knowledge analysis,

Source: Internet
Author: User

Python slice knowledge analysis,

Slice prototype strs = 'abcdefg'

Strs [start: end: step]

The three parameters of the slice are table start, end, and step size respectively.

The first subscript is 0, and the end bit is not obtained, for example, strs [] = 'bc'

If start and end are out of the range of the existing array, truncate strs [-100:100] = 'abcdef' according to the actual range'

When Step is blank, the default value is 1.

Strs [] = 'bcde' strs [] = 'bd'

When Step is set to timing, start <end, otherwise it is null

Strs [5:1] =''

Start is blank. The default value is negative infinity strs [: 4] = 'abcd'

End is blank. The default value is infinite strs [2:] = 'cdefg'

Strs [:] = 'abcdefg'

If Step is negative, start> end; otherwise, null
Strs [1: 5:-1] =''

Start is blank. The default value is infinite strs [: 2:-1] = 'gfed'

End is blank. The default value is negative infinity strs [4:-1] = 'edba'

Strs [:-1] = 'gfedcba'

Python Slicing

The Slice operator is a sequence name followed by a square bracket, which contains an optional number and is separated by a colon. Note that this is very similar to the index operator you are using. Remember that numbers are optional, while colons are required.

The first number (before the colon) in the slice operator indicates the start position of the slice, the second number (after the colon) indicates the end of the slice, and the third number (after the colon) indicates the slice interval. If the first number is not specified, Python starts from the beginning of the sequence. If the second number is not specified, Python stops at the end of the sequence. Note that the returned sequence starts from the starting position and ends just before the ending position. That is, the start position is included in the sequence slice, and the end position is excluded from the slice.

In this way, shoplist [] returns a sequence slice starting from position 1, including position 2, but stops at position 3. Therefore, it returns a slice containing two items. Similarly, shoplist [:] returns a copy of the entire sequence. Shoplist [: 3] Return position 3, position 6, position 9... .

You can use a negative number for slicing. A negative number is used to start from the end of the sequence. For example, shoplist [:-1] returns a sequence slice that contains all projects except the last project. shoplist [:-1] returns an Inverted Sequence slice.

Use the Python interpreter to interactively specify a combination of different slices, that is, you can see the result immediately at the prompt. The magic of sequences is that you can access tuples, lists, and strings in the same way.

Articles you may be interested in:
  • Python slice usage example tutorial
  • Python slice and range () usage instructions

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.