Today, Bian Xiao will share a summary of several usage methods of arrays in Python, which has good reference value and hopes to help everyone. Come and have a look with Bian Xiao.
Initialization of two-dimensional array
matirx_done = [[0 for i in range(0,len(matirx))]for j in range(0,len(matirx[0])]]
It is initialized to an all-zero matrix with the same size as the matrix.
Multi-level sorting of arrays
In the array idea _ collect = [[3, 1, 2], [3, 2, 1], [3, 2, 2], [3, 1, 1]], the second item is arranged first.
idea _ collect . sort(key = lambda x:(x[ 1],-x[2]))
Where x[ 1] represents the second positive sequence arrangement and -x[2] represents the third reverse sequence arrangement.
The arrangement results are [[3, 1, 2], [3, 1, 1], [3, 2, 2], [3, 2, 1]].
In a class, multiple functions use the same array without passing parameters.
As shown in the example:
Category partition:
def __init__(self):
self.num_complete = []
Define partition (self, quantity, start, end):
self.num _ compelete = num
Def partition_core (itself):
del self . num _ complete[0]
Among them, self. Num _ compete is an array that two functions in a class can call directly at the same time, but it is best to declare this array in def __init__ first.
The above summary of several methods of using arrays in Python is all that Bian Xiao shares with you.