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-D 배열은 자체 항목들이 다시 리스트인 리스트입니다.
- 격자 또는 표를 모델링합니다: 행의 열들.
- 각 내부 리스트는 하나의 행입니다.
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.
한국어
한 셀에 도달하기
- 두 개의 인덱스를 사용하십시오: 먼저 행, 다음 열.
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-D 배열을 작성하며, 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.
한국어
흔한 실수
- 두 개의 인덱스를 사용하십시오:
grid[row][col]. - 외부 루프는 행을 순회하고, 내부 루프는 열을 순회합니다.
English
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 중첩 루프 또는 두 개의 인덱스를 사용하십시오.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
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-D 리스트 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. · 중첩된 루프를 사용하여 별 3x3 사각형을 출력합니다: 총 3줄, 각 줄에 * 문자가 3개씩 포함됩니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.