2-D arrays · Matrices 2-D
A grid of rows and columns
- A 2-D array is a list whose items are themselves lists.
- It models a grid or table: rows of columns.
- Each inner list is one row.
Una cuadrícula de filas y columnas
- Un array 2-D es una lista cuyos elementos son a su vez listas.
- Modela una cuadrícula o tabla: filas de columnas.
- Cada lista interna es una fila.
grid = [[1, 2, 3],
[4, 5, 6]]
print(grid)
Reach one cell
- Use two indexes: first the row, then the column.
grid[row][col]— both count from 0 in Python.
Acceder a una celda
- Utiliza dos índices: primero la fila, luego la columna.
grid[row][col]— ambos cuentan desde 0 en Python.
grid = [[1, 2, 3], [4, 5, 6]]
print(grid[0][1]) # 2
print(grid[1][2]) # 6
A nested loop visits every cell
- The outer loop walks the rows; the inner loop walks the columns.
for row in grid:gives one row (a list) at a time.
Un bucle anidado visita cada celda
- El bucle exterior recorre las filas; el bucle interior recorre las columnas.
for row in grid:proporciona una fila (una lista) a la vez.
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
Add up a grid
- Keep a running
totaland add every cell with a nested loop.
Sumar una cuadrícula
- Mantén un
totalacumulativo y suma cada celda con un bucle anidado.
grid = [[1, 2, 3], [4, 5, 6]]
total = 0
for row in grid:
for value in row:
total = total + value
print(total) # 21
In the exam: ARRAY[1:rows, 1:cols]
- Pseudocode writes a 2-D array with two ranges, counting from 1.
En el examen: ARRAY[1:filas, 1:columnas]
- La pseudocódigo escribe un array 2-D con dos rangos, contando desde 1.
DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
FOR R ← 1 TO 2
FOR C ← 1 TO 3
OUTPUT Grid[R, C]
NEXT C
NEXT R
Common mistakes
- Use two indexes:
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
Errores comunes
- Usa dos índices:
grid[row][col]. - El bucle exterior recorre las filas, el bucle interior las columnas.
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
Ahora tú lo intentas
- Utiliza un bucle anidado o dos índices.
- Pulsa Check answer para probar tu código.
A 2-D array is a grid · Una matriz 2-D es una cuadrícula
Rows of columns; reach a cell with grid[row][col]. · Filas de columnas; accede a una celda con grid[row][col].
From the grid, store the value in row 1, column 2 in a variable x (the answer is 6). · De la cuadrícula, almacena el valor en la fila 1, columna 2 en una variable x (la respuesta es 6).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · Suma todos los números en la lista 2-D grid con un bucle anidado. Almacena el total en total (la respuesta es 21).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · Usa un bucle anidado para imprimir un cuadrado de estrellas de 3 por 3: tres líneas, cada una con tres caracteres *.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.