Python notes list functions and methods use examples

Source: Internet
Author: User
Tags string format

Learn the difference between raw_input and input before learning the list: (Only for version 2, version 3, raw_input and input merge, no raw_input)

The syntax for input is: input ("str").

The Raw_input function saves all input as the original data in a string format;

The input Default user entry is a valid Python format and has some limitations when it comes to string connections.

1. Sequence Index

There are two types of expressions for a sequence index in Python:

(1) The first element index is 0, the second one is 1, and so on;

(2) The last element index is-1, the second to the bottom is-2, and so on;

Thus, assuming that the number of sequences A is n, then for the first element: A[i]=a[-n+i]

2. Sequence Shards

A shard is an extension of an index that accesses a single element, and the Shard accesses a range of elements. The Shard needs to provide two indexes as a boundary, the elements of the first index are contained within the Shard, and the second is not contained within a shard.

Example:

>>> number=[0,1,2,3,4,5,6,7,8,9]
>>> Number[2:5]
[2, 3, 4]

The representation of the step size:

>>> Number[2:9:2]
[2, 4, 6, 8]
>>> Number[2:8:2]
[2, 4, 6]

function: Len: The number of elements, and Min and Max return the smallest and largest elements in the sequence, respectively.

3. Basic list Operations

First introduce the list function: Split the string into an array format

>>> list ("list")

[' l ', ' I ', ' s ', ' t ']

(1) Element assignment:

>>> number=[0,1,2,3,4,5,6,7,8,9]

>>> number[0]=10
>>> number
[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]

(2) Deleting an element

>>> number=[0,1,2,3,4,5,6,7,8,9]

>>> del Number[0]
>>> number
[1, 2, 3, 4, 5, 6, 7, 8, 9]

(3) Shard Assignment

>>> number[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> number[0:]=[1,2,3]
>>> number
[1, 2, 3]

4. How to List

(1) Append: Add a new element at the end of the list

>>> number=[1, 2, 3]

>>> Number.append (5)
>>> number
[1, 2, 3, 5]

(2) Count: Count the number of occurrences of an element

>>> number=[1, 2, 3]

>>> Number.count (1)
1

(3) Extend: Adding a sequence after a sequence

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> A.extend (b)
>>> A
[1, 2, 3, 4, 5, 6]

Python notes list functions and methods use examples

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.