1 List and NP reciprocal conversion
A single list can be converted to an NP array, and multiple class tables can be converted to NP matrices (multidimensional arrays):
1 #Import the data processing package2 ImportNumPy as NP3 4X = [1,2,3,4,5]5Y = [5,4,3,2,1]6 #Normal list converted to NP array7x = Np.array (x, dtype=np.int8)8 Print(X)9 #can be multi-dimensional combinationTenXY = Np.array ([x, y], dtype=np.int8) One Print(XY) A #after conversion to a NP array, the matrix can be computed as follows: Matrix subtraction -x_y = Np.array (X, Dtype=np.int8)-Np.array (Y, dtype=np.int8) - Print(x_y) the #you can count the number of specified elements in the NP array, as follows: Count the number of elements in the x_y matrix with an absolute value of 2 -X_y_2 = Np.sum (Np.fabs (x_y) ==2) - Print(x_y_2)
The 0 matrix can be generated using NP:
1 #Import the data processing package2 ImportNumPy as NP3 4 #NP-generated 0 matrix5Zero = Np.zeros ([4,5], np.int8)#Create a 4*5 0 matrix6 Print(Zero)7 #Python also has a way to generate 0 matrices without using NP packets.8Zero = [[0 forIinchRange (5)] forIinchRange (4)]#Create a 4*5 0 matrix9 Print(Zero)
2 str, list, dict and other operations
The STR numeric list is converted to an int numeric list:
1 #str numeric list to int list2X = ['1','2','3','4','5']3 Print(X)4 #STR Numeric list cannot be parameterized numeric operation, need to convert to int numeric list5X =list (map (int, X))6 PrintX
Two lists to synthesize a dictionary:
1 #two lists combined into a dictionary2X = ['X1','X2','X3','X4','X5']3Y = [1,2,3,4,5]4 #generate a dictionary, x as key,y as value, each corresponding5Z_dict =dict (Zip (x, y))6 Print(z_dict)
Sort the elements in the dictionary (note that the dict becomes a list after sorting):
1 #sort the value in the dictionary from the big to the small, converting it to a list form2Z_dict = {'X1': 1,'X2': 2,'X3': 3,'X4': 4,'X5': 5}3Z_list = sorted (Z_dict.items (), key =LambdaX:X[1], reverse = True)#Note that after sorting, Dict becomes a list4 Print(z_list)
Python's list, Dict, NP, and other common numerical operations