Multidimensional arrays and matrix arithmetic output operation example Python

This article describes the Python Examples multidimensional array output operation and matrix operation. Share to you for your reference, as follows:

In many programming languages ​​(Java, COBOL, BASIC), a multi-dimensional array or matrix (defining the size of each dimension) predefined. In Python, its implementation easier.

If you need to deal with more complex situations, you may need to use Python math module package NumPy, link address: http: //numpy.sourceforge.net/

First look at a simple two-dimensional form. There are 36 possible outcomes when throwing two dice. We can be made into a two-dimensional table, thanks to a number of rows and columns representing a dice:

. 1 2. 3. 4. 5. 6
. 1 2. 3. 4. 5. 6. 7
2. 3. 4. 5. 6. 7. 8
. 3. 4. 5. 6. 7. 8. 9
. 4. 5. 6. 7. 8. 9 10
. 5. 6. 7. 8. 9 10. 11
. 6. 7. 8. 9 10. 11 12 is
in Python in a multi-dimensional forms can be like the "sequence of" implementation. A table is a sequence of lines. Each row is a sequence of individual cells. This is similar to our use of mathematical notation, in mathematics we use Ai, j, and in Python we use the A [i] [j], on behalf of the i-th row of the matrix column j.

It looks very much like "list of tuples" (Lists of Tuples).

"List of lists" Examples

Create a form we can use nested list comprehensions (list comprehension). The following example creates a table "sequences of" configuration, and assigned to each cell of the table.

table= [[0 for i in range(6)] for j in range(6)]
print(table)
for d1 in range(6):
  for d2 in range(6):
    table[d1][d2]= d1+d2+2
print(table)

The output of the following program:
Here Insert Picture Description
this program does two things: create a table full 0 of 6 × 6. Then use a combination of two dice may be filled with values of the table. This is not to complete the most efficient way, but we have to demonstrate several techniques through this simple example. We take a closer look before and after the two parts of the program.

The first part of the creation of the program and outputs a list containing six elements, which we call "form"; table each element is a list containing the elements 6 0. It uses the derived type list, for the range from 0 to j 6 are each created object. Each object is a list of elements 0, generated by the variable i from 0 to 6 traversal. After initialization is complete, the two-dimensional printout of all 0s form.

Derivations can be read from the inside out, just as a general expression. Inner list [0 for i in range (6)] to create a simple list contains 0 to 6. Outer list [[...] for j in range (6)] creates six copies of these deep inner lists.

The second part of the program for each combination of two dice iteration, each cell is filled the table. This is achieved by the two nested loops, a loop iteration every dice. The outer loop enumerator to the first dice of all possible values ​​d1. The second inner loop enumeration dice d2.

Need updating each cell table [d1] selects each row; This is a list containing six values. The cells selected in the list selected by ... [d2]. We value assigned to this cell dice, d1 + d2 + 2

Other examples

Print out a list of lists is not easy to read. The following table displays the cycle in a more readable form.

>>> 
for row in table:
... 
  print row
... 
[2, 3, 4, 5, 6, 7]
[3, 4, 5, 6, 7, 8]
[4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10, 11]
[7, 8, 9, 10, 11, 12]

As an exercise, the reader can try to print a list of the contents, then print the travel and header columns. Reminder, using "% 2d"% value string operator may print out the fixed length digital format.

Display index value (Explicit Index Values)

Our next pair of dice table summary statistics, obtained cumulative frequency table. We use 13 contains a list of elements (index from 0 to 12) represents the frequency of occurrence of each of the dice value. Observe that the value of the dice 2 appears only once in the matrix, we expect FQ [2] a value of 1. Through each cell in the matrix, the obtained cumulative frequency table.

fq= 13 * [0]
for i in range(6):
  for j in range(6):
    c= table[i][j]
    fq[ c ] += 1

The subscript i in the selected table rows from the row selected by a subscript j, to obtain cell c. Then fq statistical frequency.

It looks very mathematical and norms. Python provides another simpler way.

Instead of using the list iterator index

Table is a list of lists can be used without the subject for the next loop through the list of elements.

fq= 13 * [0]
print fq
for row in table:
  for c in row:
    fq[c] += 1
print fq[2:]

Mathematical matrix

We used the "standard display" Operation mathematical definition of technology matrix. Matrix can be done in this way more clearly. In this presentation we achieve matrix addition.

m1 = [[1, 2, 3, 0], [4, 5, 6, 0], [7, 8, 9, 0]]
m2 = [[2, 4, 6, 0], [1, 3, 5, 0], [0, -1, -2, 0]]
m3 = [4*[0] for i in range(3)]
for i in range(3):
  for j in range(4):
    m3[i][j] = m1[i][j] + m2[i][j]
print(m3)

operation result:

[[3, 6, 9, 0], [5, 8, 11, 0], [7, 7, 7, 0]]

In this example, we created two input matrices m1 and M2, are each a 3 × 4 matrix. Then use the list to derive initialized third row 0 3 m3 4 column matrix. Then we use the variable i through each row, each column traversed using the variable j to calculate and m1 and m2.

Content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning

Skills, learning experience, interview skills, workplace experience and other share, the more carefully prepared the zero-based introductory information, information on actual projects,

The method has timed programmer Python explain everyday technology, to share some of the learning and the need to pay attention to small details
Here Insert Picture Description

Published 49 original articles · won praise 8 · views 40000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105082486