2-D arrays · 2次元配列
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
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.
日本語
行と列のグリッド
- 2次元配列は、その要素自体がリストであるリストです。
- グリッドや表をモデル化します:列の行。
- 各内側リストは1つの行を表します。
grid = [[1, 2, 3],
[4, 5, 6]]
print(grid)
English
Reach one cell
- Use two indexes: first the row, then the column.
grid[row][col]— both count from 0 in Python.
日本語
1つのセルにアクセス
- 2つのインデックスを使用します:まず行、次に列。
grid[row][col]— Pythonではどちらも0から数えます。
grid = [[1, 2, 3], [4, 5, 6]]
print(grid[0][1]) # 2
print(grid[1][2]) # 6
English
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.
日本語
二重ループで全セルを訪問
- 外側のループは行を、内側のループは列を遍历します。
for row in grid:は一度に一行(リスト)を返します。
grid = [[1, 2], [3, 4]]
for row in grid:
for value in row:
print(value)
English
Add up a grid
- Keep a running
totaland add every cell with a nested loop.
日本語
グリッドの合計
- 累積和
totalを維持し、二重ループで全てのセルを加算します。
grid = [[1, 2, 3], [4, 5, 6]]
total = 0
for row in grid:
for value in row:
total = total + value
print(total) # 21
English
In the exam: ARRAY[1:rows, 1:cols]
- Pseudocode writes a 2-D array with two ranges, counting from 1.
日本語
試験対策: ARRAY[1:rows, 1:cols]
- 疑似コードでは2つの範囲で2次元配列を書き、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
English
Common mistakes
- Use two indexes:
grid[row][col]. - The outer loop walks the rows, the inner loop the columns.
日本語
よくあるミス
- 2つのインデックスを使用します:
grid[row][col]。 - 外側ループは行をトレースし、内側ループは列をトレースします。
English
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
日本語
あなたも試してみよう
- 二重ループまたは2つのインデックスを使用します。
- 回答を確認 を押してコードを試してください。
Explore · 探索
A 2-D array is a grid · 2次元配列はグリッドである
Rows of columns; reach a cell with grid[row][col]. · 行列の列;**grid[row][col]**でセルにアクセスします。
From the grid, store the value in row 1, column 2 in a variable x (the answer is 6). · グリッドから、行 1、列 2 の値を変数 x に格納してください(答えは 6 です)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Add up every number in the 2-D list grid with a nested loop. Store the total in total (the answer is 21). · 2次元リストgridのすべての数値を二重ループで合計してください。総計をtotalに格納してください(答えは21です)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a nested loop to print a 3-by-3 square of stars: three lines, each with three * characters. · ネストされたループを使って、3行×3列の星印を出力する。各行に * 文字が3つ入る。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。