author : Han Xinzi @ShowMeAI
Tutorial address :http://www.showmeai.tech/tutorials/56
This paper addresses :http://www.showmeai.tech/article-detail/77
Statement : copyright , For reprint, please contact the platform and the author and indicate the source
Sequence is Python The most basic and common data structure in . Each element in the sequence is assigned a number - 【 Its location , Or index 】, The first index is 0, The second index is 1, And so on .
The operations that can be performed by a sequence include indexing , section , Add , ride , Check members .
Besides ,Python There's a built-in way to determine the length of the sequence and determine the maximum and minimum elements .
Lists are the most commonly used Python data type , It can appear as a comma separated value in square brackets .
The data items of a list do not need to have the same type .
Create a list , Just enclose the different data items separated by commas in square brackets . As shown below :
list1 = ['python', 'ShowMeAI', 1997, 2022] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"]
Just like the index of a string , List index from 0 Start . The list can be intercepted 、 Combination etc. .
Use the subscript index to access the values in the list , Similarly, you can also use square brackets to intercept the sub list .
Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
list1 = ['python', 'ShowMeAI', 1997, 2022] list2 = [1, 2, 3, 4, 5, 6, 7 ] print("list1[0]: ", list1[0]) print("list2[1:5]: ", list2[1:5])
The above code execution result :
list1[0]: python list2[1:5]: [2, 3, 4, 5]
Here is an example code ( The code can be in On-line python3 Environmental Science Run in ):
list = ['red', 'green', 'blue', 'yellow', 'white', 'black'] print( list[-1] ) print( list[:-2] ) print( list[-3:] )
Running results
black ['red', 'green', 'blue', 'yellow'] ['yellow', 'white', 'black']
You can modify or update the data items in the list , You can also use append() Method to add a list item , As shown below ( The code can be in On-line python3 Environmental Science Run in ):
list = [] ## An empty list list.append('Google') ## Use append() Additive elements list.append('ShowMeAI') print(list)
The above code execution result :
['Google', 'ShowMeAI']
have access to del Statement to delete the elements of the list , As shown below ( The code can be in On-line python3 Environmental Science Run in ):
list1 = ['python', 'ShowMeAI', 1997, 2022] print(list1) del list1[2] print(" Delete index as 2 After the element : ") print(list1)
The above code execution result :
['python', 'ShowMeAI', 1997, 2022] Delete index as 2 After the element : ['python', 'ShowMeAI', 2022]
Be careful : We'll talk about it later remove() Use of methods
The list is right + and The operators of are similar to strings .+ Numbers are used to combine lists , The number is used to repeat the list .
As shown below :
Python expression
result
describe
len(1, 2, 3)
3
length
1, 2, 3 + 4, 5, 6
1, 2, 3, 4, 5, 6
Combine
'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 Examples of list interception are as follows :
>>>L = ['Google', 'ShowMeAI', 'Baidu'] >>> L[2] 'Baidu' >>> L[-2] 'ShowMeAI' >>> L[1:] ['ShowMeAI', 'Baidu'] >>>
describe :
Python expression
result
describe
L2
'Baidu'
Read the third element in the list
L-2
'ShowMeAI'
Read the next to last element in the list
L1:
'ShowMeAI', 'Baidu'
Start with the second element to intercept the list
Python Contains the following functions :
Serial number
function
effect
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)
Converts a tuple to a list
# Example 1: length list1, list2 = [123, 'ShowMeAI', 'google'], [456, 'abc'] print(" The first 1 List length : ", len(list1)) print(" The first 2 List length : ", len(list2))
result
The first 1 List length : 3 The first 2 List length : 2
# Example 2: Max min list1, list2 = ['Baidu', 'ShowMeAI', 'google'], [456, 789, 200] print(" The first 1 Maximum number of lists : ", max(list1)) print(" The first 1 Minimum of two lists : ", min(list1)) print(" The first 2 Maximum number of lists : ", max(list2)) print(" The first 2 Minimum of two lists : ", min(list2))
result
The first 1 Maximum number of lists : google The first 1 Minimum of two lists : Baidu The first 2 Maximum number of lists : 789 The first 2 Minimum of two lists : 200
# Example 3: To list aTuple = (123, 'ShowMeAI', 'google', 'Baidu'); aList = list(aTuple) print(" List elements : ") print(aList)
result
List elements : [123, 'ShowMeAI', 'google', 'Baidu']
Python Contains the following methods :
Serial number
Method
effect
1
list.append(obj)
Add a new object at the end of the list
2
list.count(obj)
Count the number of times an element appears in a list
3
list.extend(seq)
Appends multiple values from another sequence at once at the end of the list ( Extend the original list with the new list )
4
list.index(obj)
Find the index position of the first match of a value in the list
5
list.insert(index, obj)
Insert objects into the list
6
list.pop([index=-1])
Removes an element from the list ( Default last element ), And returns the value of that element
7
list.remove(obj)
Removes the first match of a value in the list
8
list.reverse()
Reverse list of elements
9
list.sort(cmp=None, key=None, reverse=False)
Sort the original list
aList = ['Baidu', 'ShowMeAI', 'google'] print("aList : ", aList) aList.append( 'ByteDance' ) aList += ['ShowMeAI'] print(" after append and + Calculated aList : ", aList) print(" Statistics ShowMeAI Number : ", aList.count('ShowMeAI')) aList.extend(['Taobao', 'Tencent']) print(" after extend After aList : ", aList) print(" Use index lookup Taobao Index position of : ", aList.index('Taobao')) aList.insert( 3, 'DiDi') print(" stay index by 3 The location of insert Elemental aList : ", aList) print("aList pop Out of the elements : ", aList.pop()) aList.remove('ShowMeAI') print("aList use remove Delete the first 1 Matches ShowMeAI after : ", aList) aList.reverse() print("aList Use reverse The result of reverse order : ", aList) aList.sort() print("aList Use sort The result after sorting : ", aList)
result
aList : ['Baidu', 'ShowMeAI', 'google'] after append and + Calculated aList : ['Baidu', 'ShowMeAI', 'google', 'ByteDance', 'ShowMeAI'] Statistics ShowMeAI Number : 2 after extend After aList : ['Baidu', 'ShowMeAI', 'google', 'ByteDance', 'ShowMeAI', 'Taobao', 'Tencent'] Use index lookup Taobao Index position of : 5 stay index by 3 The location of insert Elemental aList : ['Baidu', 'ShowMeAI', 'google', 'DiDi', 'ByteDance', 'ShowMeAI', 'Taobao', 'Tencent'] aList pop Out of the elements : Tencent aList use remove Delete the first 1 Matches ShowMeAI after : ['Baidu', 'google', 'DiDi', 'ByteDance', 'ShowMeAI', 'Taobao'] aList Use reverse The result of reverse order : ['Taobao', 'ShowMeAI', 'ByteDance', 'DiDi', 'google', 'Baidu'] aList Use sort The result after sorting : ['Baidu', 'ByteDance', 'DiDi', 'ShowMeAI', 'Taobao', 'google']
Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition
https://www.bilibili.com/video/BV1yg411c7Nw
The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , Babies who can visit foreign websites can also directly use google colab One click operation and interactive operation learning Oh !
This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :