Python Basic---List

Source: Internet
Author: User

Lists are the most basic data structures in Python, and the list is the most commonly used Python data type, and the list's data items do not need to have the same type. Each element in the list is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
The list of operations that can be performed includes indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.

Characteristics

A set of ordered items
. variable data type "can be added and censored"
. The list can contain any data type, or it can contain another list "can be nested in any combination"
The. List is a collection of data surrounded by square brackets "[]", with different members separated by ","
The list can be accessed by ordinal members

For example:

list = [' C ', 1, (' A ', ' B '), [[+]]

>>> Print List[1]

1

>>> Print list[2]

(' A ', ' B ')

>>> Print List[3]

[1, 2, 3]


Method of List

List.append (Var) #追加元素

List.insert (Index,var)

List.pop (Var) #返回最后一个元素 and removed from the Listist

List.remove (Var) #删除第一次出现的该元素

List.count (Var) #该元素在列表中出现的个数

List.index (Var) #该元素的位置, no exception thrown

List.extend (listist) #追加listist, that is, merging listist onto list

List.sort () #排序

List.reverse () #倒序

A[1:] #片段操作符, for extraction of sub-listist

[1,2]+[3,4] #为 [1,2,3,4]. With Extend ()

[2]*4 #为 [2,2,2,2]

Delist List[1] #删除指定下标的元素

Delist List[1:3] #删除指定下标范围的元素

Replication of Listist

List1 = List #list1为list的别名, in C is the same pointer address, the LIST1 operation is the list operation

List1 = list[:] #生成list的一个COPY

actions of built-in functions on a list

CMP (List1, List2) #比较两个列表的元素
Len (list) #列表元素个数
Max (list) #返回列表元素最大值
Min (list) #返回列表元素最小值
List (seq) #将元组转换为列表



Python Basic---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.