Home > Article > Backend Development > How to convert lists, arrays, and matrices to each other in python
This time I will bring you how to convert lists, arrays, and matrices to each other in python, and precautions for converting lists, arrays, and matrices to each other in python. What are they? The following is a practical case. Let’s take a look.
Familiarize yourself with the process through code:
# -*- coding: utf-8 -*- from numpy import * a1 =[[1,2,3],[4,5,6]] #列表 print('a1 :',a1) #('a1 :', [[1, 2, 3], [4, 5, 6]]) a2 = array(a1) #列表 -----> 数组 print('a2 :',a2) #('a2 :', array([[1, 2, 3],[4, 5, 6]])) a3 = mat(a1) #列表 ----> 矩阵 print('a3 :',a3) #('a3 :', matrix([[1, 2, 3],[4, 5, 6]])) a4 = a3.tolist() #矩阵 ---> 列表 print('a4 :',a4) #('a4 :', [[1, 2, 3], [4, 5, 6]]) print(a1 == a4) #True a5 = a2.tolist() #数组 ---> 列表 print('a5 :',a5) #('a5 :', [[1, 2, 3], [4, 5, 6]]) print(a5 == a1) #True a6 = mat(a2) #数组 ---> 矩阵 print('a6 :',a6) #('a6 :', matrix([[1, 2, 3],[4, 5, 6]])) print(a6 == a3) #[[ True True True][ True True True]] a7 = array(a3) #矩阵 ---> 数组 print('a7 :',a7) #('a7 :', array([[1, 2, 3],[4, 5, 6]])) print(a7 == a2) #[[ True True True][ True True True]] ################################################################### a1 =[1,2,3,4,5,6] #列表 print('a1 :',a1) #('a1 :', [1, 2, 3, 4, 5, 6]) a2 = array(a1) #列表 -----> 数组 print('a2 :',a2) #('a2 :', array([1, 2, 3, 4, 5, 6])) a3 = mat(a1) #列表 ----> 矩阵 print('a3 :',a3) #('a3 :', matrix([[1, 2, 3, 4, 5, 6]])) a4 = a3.tolist() #矩阵 ---> 列表 print('a4 :',a4) #('a4 :', [[1, 2, 3, 4, 5, 6]]) #注意!!有不同 print(a1 == a4) #False a8 = a3.tolist()[0] #矩阵 ---> 列表 print('a8 :',a8) #('a8 :', [1, 2, 3, 4, 5, 6]) #注意!!有不同 print(a1 == a8) #True a5 = a2.tolist() #数组 ---> 列表 print('a5 :',a5) #('a5 :', [1, 2, 3, 4, 5, 6]) print(a5 == a1) #True a6 = mat(a2) #数组 ---> 矩阵 print('a6 :',a6) #('a6 :', matrix([[1, 2, 3, 4, 5, 6]])) print(a6 == a3) #[[ True True True True True True]] a7 = array(a3) #矩阵 ---> 数组 print('a7 :',a7) #('a7 :', array([[1, 2, 3, 4, 5, 6]])) print(a7 == a2) #[[ True True True True True True]]
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!
Recommended reading:
How to assign uniform values to array elements in numpy
How to multiply numpy arrays and matrices use
The above is the detailed content of How to convert lists, arrays, and matrices to each other in python. For more information, please follow other related articles on the PHP Chinese website!