Shards and truncation sequences in Python

Source: Internet
Author: User
Sequence Concepts

In a shard rule, list, tuple, and STR (strings) can all be called sequences and can be sliced by rules.

Slicing operations

Note that the subscript 0 of the slice represents the first element of the order, 1 represents the first element in reverse, and the slice does not include the right boundary, such as [0:3] representing elements 0, 1, and 2, excluding 3.

L=[' A ', ' B ', ' C ', ' d ', 5]

1. Get the first 3 elements of a list

>>> l[0:3][' A ', ' B ', ' C ']>>> l[:3][' A ', ' B ', ' C ']

2. Get the second 3 elements of the list

>>> l[-3:][' C ', ' d ', 5]

Because the list does not include the right boundary, the right boundary of the last three elements is not specified.

3. Get all elements

>>> l[:][' A ', ' B ', ' C ', ' d ', 5]>>> l[0:][' A ', ' B ', ' C ', ' d ', 5]

4. Specify the growth step

>>> l=list (range) >>> l[0:101:10][0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Specify 10 steps for a unit

Other slices

#!/usr/bin/env python3#-*-coding:utf-8-*-vlist=[' A ', ' B ', ' C ']vtuple= (' A ', ' B ', ' C ') vstr= ' abc ' Print (Vlist[0:2]); Print (Vtuple[0:2]);p rint (Vstr[0:2])

The output is:

======================== restart:c:/python35/list.py ========================[' A ', ' B '] (' A ', ' B ') AB

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.