This article mainly introduced the Python expression matrix method, combined with the concrete example form to analyze the Python expression matrix method and the related operation attention matters, the need friend can refer to the next
The example in this article describes how Python represents a matrix. Share to everyone for your reference, as follows:
In the C language, a matrix representing "Integer 3 rows and 4 columns" can be declared as follows: Int a[3][4]; in Python, you cannot declare a variable int, and two cannot list the number of dimensions. Can be represented by a list of entrained lists. For example:
Represents a matrix, so you can:
Count = 1a = []for i in range (0, 3): TMP = [] for J in range (0, 3): tmp.append (count) count + = 1 a.app End (TMP) print a
Results:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
But note: initialization (Assignment is all 0 o'clock), the following is wrong!!
TMP = []for j in range (0, 3): tmp.append (0) A = []for i in range (0, 3): a.append (TMP) print a
Results:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Cause: This list of TMP for the same, change any row, the other lines will be given to change, pay attention!! , the following is correct:
A = []for i in range (0, 3): TMP = [] for J in range (0, 3): tmp.append (0) a.append (TMP) print a