مصفوفات 2-D
تحتاج هذه الصفحة إلى متصفح حديث (يدعم 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.
English
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.
English
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.
English
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.
English
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, usually counting from 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.
English
Now you try
- Use a nested loop or two indexes.
- Press Check answer to test your code.
استكشف
مصفوفة 2-D هي شبكة
صفوف وأعمدة؛ تصل إلى خلية باستخدام grid[row][col].
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
من الشبكة، ذخّر القيمة الموجودة في الصف 1، العمود 2 في المتغير x (الإجابة هي 6).
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
اجمع كل رقم في قائمة 2-D grid باستخدام حلقة متداخلة. احفظ المجموع في total (الجواب هو 21).
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
استخدم حلقة متداخلة لطباعة مربع من النجوم بحجم 3×3: ثلاثة أسطر، كل سطر يحتوي على * ثلاث.
اضغط تشغيل لرؤية المخرجات هنا.