Python3 list
Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on.
Python has 6 built-in types for sequences, but the most common are lists and tuples.
Operations that can be performed on sequences include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.
List is the most commonly used Python data type, which can appear as a comma-separated value within square brackets.
The data items in the list do not need to be of the same type
To create a list, just use square brackets to enclose the different data items separated by commas. It looks like this:
list1 = ['Google', 'php', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"];
Like string indexing, list indexing starts at 0. Lists can be intercepted, combined, etc.
Access the values in the list
Use subscript index to access the values in the list. You can also use square brackets to intercept characters, as shown below:
#!/usr/bin/python3 list1 = ['Google', 'php', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])
The above example output result:
list1[0]: Google list2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update the data items in the list, you can also use append () method to add list items, as shown below:#!/usr/bin/python3 list = ['Google', 'php', 1997, 2000] print ("第三个元素为 : ", list[2]) list[2] = 2001 print ("更新后的第三个元素为 : ", list[2])
Note: We will discuss the use of the append() method in the next chapter
Output results of the above example:
第三个元素为 : 1997 更新后的第三个元素为 : 2001
Delete list elements
You can use the del statement to delete elements of the list, as shown in the following example:
#!/usr/bin/python3 list = ['Google', 'php', 1997, 2000] print (list) del list[2] print ("删除第三个元素 : ", list)
The output result of the above example:
删除第三个元素 : ['Google', 'php', 2000]
Note: We will discuss the use of the remove() method in the next chapter
Python list script operator
List operations on + and * Characters are similar to strings. The + sign is used for combined lists, and the * sign is used for repeated lists.
looks like this:
Python expression | Result | Description |
---|---|---|
len([1, 2, 3]) | 3 | Length |
[1, 2, 3] + [4 , 5, 6] | [1, 2, 3, 4, 5, 6] | combination |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | Repeat |
3 in [ 1, 2, 3] | True | Whether the element exists in the list |
for x in [1, 2, 3]: print x , | 1 2 3 | Iteration |
##Python list interception and splicingPython list interception with string operation types, as follows:
L=['Google', 'php', 'Taobao']Operation:
Result | Description | |
---|---|---|
'Taobao' | Read the third element | |
'php' | Read the second to last element from the right: count from the right | |
['php', 'Taobao'] | Output all elements starting from the second element |
Serial number | Function |
---|---|
1 | len(list) Number of elements in the list |
2 | max(list) Return list Maximum value of the element |
3 | min(list) Returns the minimum value of the element in the list |
4 | list(seq) Convert tuple to list |
Python contains the following methods:
Serial number | Method |
---|---|
list.append(obj) | Add a new object at the end of the list |
list.count(obj) | Count the number of times an element appears in the list |
list.extend(seq) | Append multiple values from another sequence at the end of the list (extend the original list with a new list) |
list.index(obj) | Find the index position of the first matching item of a value from the list|
list.insert( index, obj) | Insert objects into the list|
list.pop(obj=list[-1]) | Remove objects from the list An element (default last element), and returns the value of the element|
list.remove(obj) | Remove a value in the list The first match of|
list.reverse() | Reverse the element in the list|
list.sort([func]) | Sort the original list|
list.clear() | Clear the list|
list.copy() | Copy list