Alterna · учебный материал
Two-dimensional arrays
38.2: Two-dimensional arrays: Short theory, one example, and Python stepway.
Что в этом материале
- How it works.
- One example in execution
- Remember
Содержание и основные понятия
The matrix is a list of lines. The first index selects the row, the second the column: matrix[row][column].
substance and practice
Grade 9
EEG No. 17
EEG No. 26
01
How it works.
The matrix is a list of lines. Two indices are always read in the same order: first a line, then a column.
Form.
For a rectangular matrix n = Len(matrix), m = Len (matrix[0]).
Access.
matrix[row] [column] first selects the line, then the element in it.
Independent lines.
Each line should be a separate list.
It's a round.
The outer cycle selects a line, an inner column or an element of a line.
02
One example in execution
Start the short program and go over the lines. The visualiser only shows the code, current line, variables and output.
Expression
[[0] * m] * n
Creates n links to one line: the change of one cell will be repeated in all rows. The correct design uses a list display and creates a new line for each repeat. The Diagonal of the square matrix has a condition.
row == column
; side diagonal -
row + column == n - 1
.
Algorithm map
Read from left to right, then run the code.
1
Start
2
Create independent lines
3
Select row 1
4
Amend column 2
5
Remove Matrix
03
Remember
Don't use [0] * m] *n for a variable matrix.
Check n = 0 before turning matrix[0].
For a rectangular matrix, do not replace m by the number of rows.
Syntax of this page
matrix[row][column]
Access to element: both indices should be within permissible boundaries.
len(matrix)
Number of lines.
len(matrix[0])
Number of columns with an empty rectangular matrix.
[[0] * m for _ in range(n)]
Creates n independent rows of length m.
range(n)
He's going to cross the acceptable lines.
sum(row)
The sum of one line of numbers.
print
Prints the matrix value or teaching presentation.