2-D arrays · Arrays 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.
Uma grade de linhas e colunas
- Um array 2-D é uma lista cujos itens são próprias listas.
- Modela uma grade ou tabela: linhas de colunas.
- Cada lista interna é uma linha.
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.
Acesse uma célula
- Use dois índices: primeiro a linha, depois a coluna.
grid[row][col]— ambos contam a partir de 0 em 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.
Um laço aninhado visita todas as células
- O loop externo percorre as linhas; o loop interno percorre as colunas.
for row in grid:fornece uma linha (uma lista) de cada 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.
Somar uma grade
- Mantenha um
totalacumulativo e adicione cada célula com um laço aninhado.
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.
Na prova: ARRAY[1:linhas, 1:colunas]
- Pseudocódigo escreve um array 2-D com dois intervalos, contando a partir de 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.
Erros comuns
- Use dois índices:
grid[row][col]. - O loop externo percorre as linhas, o interno as colunas.
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
Agora você tenta
- Use um laço aninhado ou dois índices.
- Clique em Check answer para testar seu código.
A 2-D array is a grid · Um array 2-D é uma grade
Rows of columns; reach a cell with grid[row][col]. · Linhas de colunas; acesse uma célula com grid[linha][coluna].
From the grid, store the value in row 1, column 2 in a variable x (the answer is 6). · Da grade, armazene o valor na linha 1, coluna 2 em uma variável x (a resposta é 6).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · Some todos os números na lista 2-D grid com um loop aninhado. Armazene o total em total (a resposta é 21).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · Use um loop aninhado para imprimir um quadrado 3x3 de estrelas: três linhas, cada uma com três caracteres *.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.