The sequence is the most basic data structure in Python.

Source: Internet
Author: User
Tags python list

A list is an ordered, repeatable set of elements that can be added and removed at any time.
The sequence is the most basic data structure in Python.
Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.
Data items for a list do not need to have the same type
Create a list by enclosing separate data items separated by commas in square brackets. As shown below:
1 list1 = [' Google ', ' Runoob ', 1997, 2000];
2 List2 = [1, 2, 3, 4, 5];
3 List3 = ["A", "B", "C", "D"];
As with the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.
Accessing values in a list
Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:

1 #!/usr/bin/python3
2
3 List1 = [' Google ', ' Runoob ', 1997, 2000];
4 List2 = [1, 2, 3, 4, 5, 6, 7];
5
6 print ("list1[0]:", list1[0])
7 print ("List2[1:5]:", List2[1:5])

The result of the above example output:
1 List1[0]: Google
2 List2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update the list's data items, or you can use the Append () method to add the list items as follows:

1 #!/usr/bin/python3
2
3 list = [' Google ', ' Runoob ', 1997, 2000]
4
5 print ("Third element:", list[2])
6 List[2] = 2001
7 Print ("The third element after the update is:", list[2])

The result of the above example output:
1 The third element is: 1997
2 The third element after the update is: 2001
1 >>> names
2 [' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ']
3 >>> names.append ("I'm new Here")
4 >>> Names
5 [' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new '

1 >>> names
2 [' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new '
3 >>> Names.insert (2, "forcibly inserted from Eric Front")
4 >>> Names
5 [' Alex ', ' Tenglan ', ' forcibly inserted ' from the front of Eric ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm the new '
6
7 >>> Names.insert (5, "insert from behind Eric try new posture")
8 >>> Names
9 [' Alex ', ' Tenglan ', ' forcibly inserted from Eric ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new '

1 >>> names
2 [' Alex ', ' Tenglan ', ' forcibly inserted from Eric ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new '
3 >>> names[2] = "It's time to substitute."
4 >>> Names
5 [' Alex ', ' Tenglan ', ' the substitution ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new '.

To delete a list element
You can use the DEL statement to delete the elements of a list, as in the following example:
1 #!/usr/bin/python3
2
3 list = [' Google ', ' Runoob ', 1997, 2000]
4
5 Print (list)
6 del list[2]
7 Print ("Delete third element:", list)

The result of the above example output:
1 [' Google ', ' Runoob ', 1997, 2000]
2 Delete the third element: [' Google ', ' Runoob ', 2000]

1 >>> del names[2]
2 >>> Names
3 [' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' insert a new pose from behind Eric ', ' Tom ', ' Amy ', ' I'm new '
4 >>> del names[4]
5 >>> Names
6 [' Alex ', ' Tenglan ', ' Eric ', ' Rain ', ' Tom ', ' Amy ', ' I'm new '
7 >>>
8 >>> names.remove ("Eric") #删除指定元素
9 >>> Names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', ' I'm new ']
>>> Names.pop () #删除列表最后一个值
12 ' I'm new here '
>>> names
[' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ']

Python List script operators
The list pairs + and operators are similar to strings. The + sign is used for the combined list, and the number is used for repeating lists. As shown below:

Python list interception and splicing
The list of Python intercepts the type of string manipulation as follows:
1 l=[' Google ', ' Runoob ', ' Taobao '
Operation:

1 >>>l=[' Google ', ' Runoob ', ' Taobao '
2 >>> l[2]
3 ' Taobao '
4 >>> L[-2]
5 ' Runoob '
6 >>> l[1:]
7 [' Runoob ', ' Taobao ']
8 >>>

The list also supports splicing operations:
1 >>>squares = [1, 4, 9, 16, 25]
2 >>> squares + [36, 49, 64, 81, 100]
3 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Nested lists
Use nested lists to create additional lists in the list, for example:

1 >>>a = [' A ', ' B ', ' C ']
2 >>> n = [1, 2, 3]
3 >>> x = [A, n]
4 >>> X
5 [[' A ', ' B ', ' C '], [1, 2, 3]]
6 >>> X[0]
7 [' A ', ' B ', ' C ']
8 >>> X[0][1]
9 ' B '

Extended

1 >>> names
2 [' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ']
3 >>> B = [+ +]
4 >>> names.extend (b)
5 >>> Names
6 [' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

Copy

1 >>> names
2 [' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]
3
4 >>> name_copy = Names.copy ()
5 >>> Name_copy
6 [' Alex ', ' Tenglan ', ' Rain ', ' Tom ', ' Amy ', 1, 2, 3]

Statistics
1 >>> names
2 [' Alex ', ' Tenglan ', ' Amy ', ' Tom ', ' Amy ', 1, 2, 3]
3 >>> names.count ("Amy")
4 2
Sort & Flip

1 >>> names
2 [' Alex ', ' Tenglan ', ' Amy ', ' Tom ', ' Amy ', 1, 2, 3]
3 >>> names.sort () #排序
4 Tra Ceback (most recent):
5 File "<stdin>", line 1, in <module>
6 typeerror:unorderable Types:in T () < STR () #3.0 The different data types cannot be sorted together, rub
7 >>> names[-3] = ' 1 '
8 >>> names[-2] = ' 2 '
9 >>> ; Names[-1] = ' 3 '
>>> names
One [' Alex ', ' Amy ', ' Amy ', ' Tenglan ', ' Tom ', ' 1 ', ' 2 ', ' 3 ']
>>> Names.sort ()
>>> names
[' 1 ', ' 2 ', ' 3 ', ' Alex ', ' Amy ', ' Amy ', ' Tenglan ', ' Tom ']

>>& Gt Names.reverse () #反转
>>> names
[' Tom ', ' Tenglan ', ' Amy ', ' Amy ', ' Alex ', ' 3 ', ' 2 ', ' 1 ']

Get subscript
1 >>> names
2 [' Tom ', ' Tenglan ', ' Amy ', ' Amy ', ' Alex ', ' 3 ', ' 2 ', ' 1 ']
3 >>> names.index ("Amy")
4 2 #只返回找到的第一个下标
Python list Functions & methods
? Python contains the following functions:
ordinal function
1 len (list)
Number of list elements
2 Max (list)
Returns the maximum value of a list element
3 min (list)
Returns the minimum value of a list element
4 list (SEQ)
Convert a tuple to a list
?

Python contains the following methods:
Ordinal method
1 list.append (obj)
Add a new object at the end of the list
2 List.count (obj)
Count the number of occurrences of an element in a list
3 List.extend (SEQ)
Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4 List.index (obj)
Find the index position of the first occurrence of a value from the list
5 List.insert (index, obj)
Inserting an object into a list
6 List.pop (Obj=list[-1])
Removes an element from the list (the last element by default), and returns the value of the element
7 List.remove (obj)
To remove the first occurrence of a value in a list
8 List.reverse ()
Reverse List of elements
9 List.sort ([func])
Sort the original list
Ten List.clear ()
Clear List
List.copy ()
Copy List

The sequence is the most basic data structure in Python.

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.