PythonPython · Chapterبەش 9 · Lectureوانە 9.6

Two-Dimensional Listsلیستە دووڕەهەندەکان

Tables and matrices as lists of rows: the row index and the column index, reading and changing one cell, filling a table from input and with random numbers, the [[0] * n] * m trap, printing a table row by row, and building tables with nested list comprehensions.

خشتە و وەک لە : و ، خوێندنەوە و گۆڕینی یەک خانە، پڕکردنەوەی خشتە لە و بە ، داوی [[0] * n] * m، چاپکردنی خشتە بە و دروستکردنی خشتە بە دوو یەک لە ناو یەکدا.

13 min read12 خولەک خوێندنەوە · 13 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Store a table as a list of rows and say what len(table) and len(table[0]) count.
  • Read and change one value with table[row][column].
  • Fill a table from input or with random numbers using two loops.
  • Explain and avoid the [[0] * n] * m trap.
  • Print a table as rows, with aligned columns.
  • خشتەیەک وەک لە هەڵبگریت و بڵێیت len(table) و len(table[0]) چی دەژمێرن.
  • یەک بە table[row][column] بخوێنیتەوە و بیگۆڕیت.
  • خشتەیەک لە یان بە بە دوو پڕ بکەیتەوە.
  • داوی [[0] * n] * m ڕوون بکەیتەوە و خۆتی لێ بپارێزیت.
  • خشتەیەک وەک چاپ بکەیت، بە ڕێکەوە.

A table inside a listخشتەیەک لە ناو لیستێکدا

Much of the data around us comes as a table: marks of students in subjects, a weekly timetable, the distances between towns, the squares of a game board. A table has rows and columns. In Python you store it as a two-dimensional list: a list whose elements are themselves lists, one list for each row.

زۆربەی ئەو لە دەوروبەرماندایە وەک خشتە دێت: نمرەی خوێندکاران لە وانەکاندا، خشتەی وانەکانی هەفتە، دووریی نێوان شارۆچکەکان، خانەکانی تەختەی یارییەک. خشتە و هەیە. لە وەک هەڵیدەگریت: کە خۆیان ، بۆ هەر .

Definitionپێناسە

A whose items are lists, used for a table or a : each inner list is one row. marks[1][2] is row 1, column 2. It is Python's way of making a .

کە دانەکانی لیستن و بۆ خشتەیەک یان بەکار دێت: هەر لیستێکی ناوەوە . marks[1][2] واتە ڕیزی 1، ستوونی 2. ئەمە ڕێگەی Pythonـە بۆ دروستکردنی .

Marks of four students in four subjects (made-up data)نمرەی چوار خوێندکار لە چوار وانەدا ( دروستکراو)
StudentخوێندکارProgrammingMathsبیرکاریEnglishئینگلیزیLogicلۆجیک
Aram85729064
Shilan78957088
Karwan92608175
Dilan66847793
The same table as a two-dimensional listهەمان خشتە وەک لیستی دووڕەهەندmarks.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
marks = [
    [85, 72, 90, 64],    # Aram:   Programming, Maths, English, Logic
    [78, 95, 70, 88],    # Shilan
    [92, 60, 81, 75],    # Karwan
    [66, 84, 77, 93],    # Dilan
]
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The outer list holds the whole table.

هەموو خشتەکە هەڵدەگرێت.

Lineدێڕی 2-5

Each line is one row: one student's four marks, always in the same subject order.

هەر دێڕێک : چوار نمرەی خوێندکارێک، هەمیشە بە هەمان ڕیزبەندیی وانەکان.

A block of flatsباڵەخانەیەکی شوقە

Finding a mark is like finding a flat in a block of flats: first the floor, then the door on that floor. marks[2][1] means floor 2 (Karwan's row), door 1 (Maths).

دۆزینەوەی نمرەیەک وەک دۆزینەوەی شوقەیەکە لە باڵەخانەیەکدا: سەرەتا نهۆمەکە، پاشان دەرگاکە لەو نهۆمەدا. marks[2][1] واتە نهۆمی 2 ( کاروان)، دەرگای 1 (بیرکاری).

Where the analogy stops workingسنووری ئەم نموونەیە

In Python the counting starts at 0 for both, and the "floors" do not even need the same number of doors: rows of different lengths are allowed. Most tables keep them equal.

لە ژماردن بۆ هەردووکیان لە 0ـەوە دەست پێ دەکات و «نهۆمەکان» پێویست ناکات هەمان ژمارەی دەرگایان هەبێت: بە درێژیی جیاواز ڕێگە پێدراون. زۆربەی خشتەکان یەکسانیان دەهێڵنەوە.

Quick checkپشکنینی خێراIn marks, what does one inner list stand for?لە marksـدا، یەک نوێنەرایەتیی چی دەکات؟

A list of rowsلیستێک لە ڕیزەکان

Because every element of marks is another list, marks is called a nested list. One index gives you a whole row; len(marks) is the number of rows, and len(marks[0]) — the length of the first row — is the number of columns.

لەبەر ئەوەی هەموو marks ترە، بە marks دەوترێت هێلانەیی (nested list). یەک تەواوت دەداتێ؛ len(marks) ژمارەی و len(marks[0]) — درێژیی یەکەم — ژمارەی .

Rows, columns and one rowڕیزەکان، ستوونەکان و یەک ڕیزrows.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
marks = [
    [85, 72, 90, 64],    # Aram:   Programming, Maths, English, Logic
    [78, 95, 70, 88],    # Shilan
    [92, 60, 81, 75],    # Karwan
    [66, 84, 77, 93],    # Dilan
]

print(len(marks))         # how many rows
print(len(marks[0]))      # how many columns (items in row 0)
print(marks[1])           # a whole row is a list
print(type(marks[1]))
Quick checkپشکنینی خێراA table has 6 rows and 3 columns. What is len(table)?خشتەیەک 6 و 3 هەیە. len(table) چەندە؟

Row index, then column indexئیندێکسی ڕیز، پاشان ئیندێکسی ستوون

A value in a two-dimensional list is reached with two indexes: matrix[i][j]. The first, the row index i, picks a row. The second, the column index j, picks a value inside that row. Read it from left to right: matrix[3] is row 3, and matrix[3][4] is item 4 of that row.

لە بە دوو دەگەیەنرێت: matrix[i][j]. یەکەمیان، i، هەڵدەبژێرێت. دووەمیان، j، لە ناو ئەو هەڵدەبژێرێت. لە چەپەوە بۆ ڕاست بیخوێنەوە: matrix[3] 3ـە و matrix[3][4] 4ـی ئەو .

The slide's 5×55 \times 5 matrixماتریکسە 5×55 \times 5ـەکەی سلایدەکەmatrix_rows.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
matrix = [
    [1, 2, 3, 4, 5],
    [6, 7, 0, 0, 0],
    [0, 1, 0, 0, 0],
    [1, 0, 0, 0, 8],
    [0, 0, 9, 0, 3],
]

for i in range(len(matrix)):
    print(f"matrix[{i}] is {matrix[i]}")
print("matrix[0][0] is", matrix[0][0])
print("matrix[4][4] is", matrix[4][4])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 9-10

One index: each matrix[i] is a whole row.

یەک : هەر matrix[i] تەواوە.

Lineدێڕی 11-12

Two indexes: one value. [0][0] is the top-left corner, [4][4] the bottom-right.

دوو : یەک . [0][0] گۆشەی سەرەوەی چەپە، [4][4] گۆشەی خوارەوەی ڕاست.

Animation on a 5 × 5 grid with row numbers 0–4 on the left and column numbers 0–4 on top. matrix[3][4]: first the whole row 3, [1, 0, 0, 0, 8], lights up; then, inside it, the cell in column 4 is marked: 8. Then matrix[4][2]: row 4 lights up, then column 2 inside it: 9.
Animation on a 5 × 5 grid with row numbers 0–4 on the left and column numbers 0–4 on top. matrix[3][4]: first the whole row 3, [1, 0, 0, 0, 8], lights up; then, inside it, the cell in column 4 is marked: 8. Then matrix[4][2]: row 4 lights up, then column 2 inside it: 9.
First the row, then the column inside it.سەرەتا ، پاشان لە ناویدا.CêIT
Quick checkپشکنینی خێراIn the slide's matrix, what is matrix[1][0]?لە matrixـی سلایدەکەدا، matrix[1][0] چییە؟

Accessing Elements of a List in PythonNeso Academy · Englishئینگلیزی · 85K viewsبینین · 1.4K likesلایک · Partبەشی 9:10–16:09 (ofلە 17:17)

How the first index picks an inner list and the second index picks a value inside it, drawn as memory boxes.

چۆن یەکەم ناوەوە هەڵدەبژێرێت و دووەم لەناو ئەودا، کە وەک سندووقی کێشراون.

Watch 9:10–16:09, 'Multidimensional List': two indexes, first the inner list, then the value.

لە ⁦9:10⁩ تا ⁦16:09⁩ سەیر بکە، بەشی Multidimensional List: دوو ، یەکەمیان بۆ ناوەوە، دووەمیان بۆ .

Row index, then column indexئیندێکسی ڕیز، پاشان ئیندێکسی ستوون

Reading and changing one cellخوێندنەوە و گۆڕینی یەک خانە

With the two indexes you can also change one value: marks[2][1] = 65 replaces Karwan's Maths mark and nothing else. Negative indexes count from the end, as in a normal list. An index that is too big stops the program with an IndexError.

بە دوو دەتوانیت بگۆڕیت: marks[2][1] = 65 نمرەی بیرکاریی کاروان دەگۆڕێت و هیچی تر. لە کۆتاییەوە دەژمێرن، وەک لە ئاساییدا. کە زۆر گەورە بێت بە IndexError ڕادەگرێت.

Read, change, and go too farبخوێنەوە، بیگۆڕە و زۆر دوور بڕۆchange_cell.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
marks = [
    [85, 72, 90, 64],    # Aram:   Programming, Maths, English, Logic
    [78, 95, 70, 88],    # Shilan
    [92, 60, 81, 75],    # Karwan
    [66, 84, 77, 93],    # Dilan
]

print("Karwan, Maths:", marks[2][1])
marks[2][1] = 65                 # change one cell
print("after the re-check:", marks[2])
print("last student, last subject:", marks[-1][-1])
print(marks[4][0])               # there is no row 4
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

[-1][-1] is the last row's last value: Dilan's Logic mark.

[-1][-1] دوایین دوایین : نمرەی لۆجیکی دیلان.

Lineدێڕی 12

Rows are numbered 0–3, so row 4 does not exist.

لە 0 تا 3 ژمارە کراون، بۆیە 4 بوونی نییە.

Quick checkپشکنینی خێراWhich expression gives Shilan's English mark?کام دەربڕین نمرەی ئینگلیزیی شیلان دەدات؟

Filling a table from inputپڕکردنەوەی خشتە لە نێوخستەی بەکارهێنەرەوە

To build a table from typed values, start with an empty list. For every row, first append an empty row [], then append the values of that row one by one with matrix[row].append(value). Two loops, one inside the other: the outer loop walks the rows, the inner loop the columns of the current row.

بۆ دروستکردنی خشتەیەک لە نووسراو، بە بەتاڵ دەست پێ بکە. بۆ هەر ، سەرەتا بەتاڵ [] زیاد بکە، پاشان ئەو یەک بە یەک بە matrix[row].append(value) زیاد بکە. دوو ، یەکێکیان لە ناو ئەوی تردا: بەسەر دەڕوات و بەسەر ئێستاکەدا.

Reading a matrix row by rowخوێندنەوەی ماتریکسێک ڕیز بە ڕیزmatrix_input.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
matrix = []                                   # create an empty list
number_of_rows = int(input("Enter the number of rows: "))
number_of_columns = int(input("Enter the number of columns: "))
for row in range(number_of_rows):
    matrix.append([])                         # add an empty new row
    for column in range(number_of_columns):
        value = int(input("Enter an element and press Enter: "))
        matrix[row].append(value)
print(matrix)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

A new empty row; it becomes matrix[row].

بەتاڵی نوێ؛ دەبێتە matrix[row].

Lineدێڕی 8

Each value goes into the row that was just added.

هەر دەچێتە ناو ئەو تازە زیاد کرا.

Quick checkپشکنینی خێراWith 3 rows and 2 columns, how many times does input run in the inner loop?بە 3 و 2 ، input لە چەند جار جێبەجێ دەبێت؟

Filling a table with random numbersپڕکردنەوەی خشتە بە ژمارە هەڕەمەکییەکان

The same two loops can fill a table with random numbers instead of typed ones — useful for testing a program on many tables quickly. random.randint(0, 99) returns a whole number from 0 to 99; both ends can appear.

هەمان دوو دەتوانن خشتەیەک لەبری ژمارەی نووسراو بە پڕ بکەنەوە — بەسوودە بۆ تاقیکردنەوەی خێرای لەسەر چەندین خشتە. random.randint(0, 99) لە 0 تا ⁦99⁩ دەگەڕێنێتەوە؛ هەردوو سەرەکەی دەکرێت دەربکەون.

A 4×54 \times 5 table of random numbersخشتەیەکی 4×54 \times 5 لە ژمارە هەڕەمەکییەکانmatrix_random.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
import random
random.seed(1)    # only to repeat this example; remove it for new numbers

matrix = []
number_of_rows = int(input("Enter the number of rows: "))
number_of_columns = int(input("Enter the number of columns: "))
for row in range(number_of_rows):
    matrix.append([])
    for column in range(number_of_columns):
        matrix[row].append(random.randint(0, 99))
print(matrix)
Quick checkپشکنینی خێراCan random.randint(0, 99) return 99?ئایا random.randint(0, 99) دەتوانێت ⁦99⁩ بگەڕێنێتەوە؟

The [[0] * n] * m trapداوی [[0] * n] * m

A tempting shortcut for an empty table is [[0] * 3] * 2. It prints like two rows of zeros, but it is one row with two names. * on a list repeats the references it holds; it does not copy the inner list. Change one cell and the "other" row changes too.

ڕێگەیەکی کورتی سەرنجڕاکێش بۆ خشتەیەکی بەتاڵ [[0] * 3] * 2ـە. وەک دوو سفر چاپ دەبێت، بەڵام یەک بە دوو ناوەوە. * لەسەر ناو دووبارە دەکاتەوە؛ کۆپی ناکات. خانەیەک بگۆڕە و «تر»یش دەگۆڕێت.

One row twice, or two rows?یەک ڕیز دوو جار، یان دوو ڕیز؟aliasing_trap.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
grid = [[0] * 3] * 2       # looks like 2 rows of 3 zeros
grid[0][0] = 5
print(grid)                 # both rows changed!
print(grid[0] is grid[1])

good = [[0] * 3 for row in range(2)]    # a NEW row on every round
good[0][0] = 5
print(good)
print(good[0] is good[1])
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

The inner [0] * 3 runs once; the outer * 2 puts two references to that one row in the list.

[0] * 3ـی ناوەوە یەکجار جێبەجێ دەبێت؛ * 2ـی دەرەوە دوو بۆ هەمان یەک دەخاتە ناو .

Lineدێڕی 6

In a list comprehension (or a loop with append), [0] * 3 runs again for every row, so each row is a separate list.

ئەگەر بەکار بهێنیت (یان بە append)، [0] * 3 بۆ هەر دووبارە جێبەجێ دەبێتەوە، بۆیە هەر جیایە.

Two panels. Left, grid = [[0] * 3] * 2: an outer list with two slots whose arrows both point to the same single row [5, 0, 0]. Right, a list comprehension: an outer list whose two arrows point to two separate rows, [5, 0, 0] and [0, 0, 0].دوو بەش. چەپ، grid = [[0] * 3] * 2: لیستێکی دەرەوە بە دوو شوێن کە تیرەکانیان هەردووکیان ئاماژە بە هەمان یەک ڕیز دەکەن: [5, 0, 0]. ڕاست، list comprehension: لیستێکی دەرەوە کە دوو تیرەکەی ئاماژە بە دوو ڕیزی جیا دەکەن: [5, 0, 0] و [0, 0, 0].
* 2 repeats a reference; the comprehension builds new rows.* 2 دووبارە دەکاتەوە؛ نوێ دروست دەکات.CêIT
Quick checkپشکنینی خێراt = [[1, 2]] * 3 and then t[2][0] = 9. What is t?t = [[1, 2]] * 3 و پاشان t[2][0] = 9. t چییە؟

Printing a tableچاپکردنی خشتە

print(matrix) shows the whole list on one line, brackets and all. To print it as a table, visit every value with two loops. The slides give two ways: with indexes (range(len(...))), or directly over the rows and their values.

print(matrix) هەموو لەسەر یەک دێڕ پیشان دەدات، لەگەڵ هەموو کەوانەکان. بۆ ئەوەی وەک خشتە چاپی بکەیت، بە دوو سەردانی هەموو بکە. سلایدەکان دوو ڕێگە دەدەن: بە (range(len(...)))، یان ڕاستەوخۆ بەسەر و .

With indexes (as on the slide)بە ئیندێکسەکان (وەک لە سلایدەکەدا)print_indexes.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]    # assume a list is given
for row in range(len(matrix)):
    for column in range(len(matrix[row])):
        print(matrix[row][column], end=" ")
Directly over rows and values (as on the slide)ڕاستەوخۆ بەسەر ڕیزەکان و نرخەکاندا (وەک لە سلایدەکەدا)print_values.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
    for value in row:
        print(value, end=" ")

With field widths from f-strings (see Nested loops) the marks table gets headings and straight columns:

بە پانیی خانە لە ⁦f-string⁩ـەوە (خولە ناوەکییەکان ببینە) خشتەی نمرەکان ناونیشان و ڕێک وەردەگرێت:

The marks table, alignedخشتەی نمرەکان، ڕێکخراوprint_marks.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
marks = [
    [85, 72, 90, 64],    # Aram:   Programming, Maths, English, Logic
    [78, 95, 70, 88],    # Shilan
    [92, 60, 81, 75],    # Karwan
    [66, 84, 77, 93],    # Dilan
]
names = ["Aram", "Shilan", "Karwan", "Dilan"]
subjects = ["Prog", "Maths", "Eng", "Logic"]

print(" " * 8, end="")
for subject in subjects:
    print(f"{subject:>7}", end="")
print()
for i in range(len(marks)):
    print(f"{names[i]:8}", end="")
    for value in marks[i]:
        print(f"{value:7}", end="")
    print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10-13

The heading row: 8 spaces, then each subject right-aligned in 7 characters.

ناونیشان: 8 بۆشایی، پاشان هەر وانەیەک لە 7 بەرەو ڕاست.

Lineدێڕی 14-18

One line per student: the name in 8 characters, then four marks of 7 characters, then print().

بۆ هەر خوێندکارێک دێڕێک: ناوەکە لە 8 ، پاشان چوار نمرە هەریەکە 7 ، پاشان print().

Quick checkپشکنینی خێراWhere must the empty print() go to print one row per line?print()ـی بەتاڵ دەبێت لە کوێ بێت بۆ ئەوەی هەر لەسەر دێڕێک چاپ بێت؟

Python 2D collections are easy! ⬜Bro Code · Englishئینگلیزی · 151K viewsبینین · 3.5K likesلایک · Partبەشی 4:24–6:11 (ofلە 8:38)

A table stored as a list of lists, printed with two nested loops, end=" " and an empty print() that ends each row.

خشتەیەک کە وەک لە هەڵگیراوە و بە ، end=" " و print()ـێکی بەتاڵ پیشان دەدرێت کە هەر کۆتایی پێدەهێنێت.

Watch 4:24–6:11: for each row, for each item, print(item, end=" "), then print() to end the row.

لە ⁦4:24⁩ تا ⁦6:11⁩ سەیر بکە: بۆ هەر و بۆ هەر ، print(item, end=" ")، پاشان print() بۆ کۆتاییهێنان بە .

Extra: building a table in one lineزیاتر: دروستکردنی خشتە بە یەک دێڕ

A list comprehension can itself contain a list comprehension. Read [[r * c for c in range(1, 5)] for r in range(1, 4)] from the outside in: "for every r from 1 to 3, make the row [r * c for c in range(1, 5)]". It is the same nested loop as before, written as an expression.

دەکرێت لە ناو دابنرێت. [[r * c for c in range(1, 5)] for r in range(1, 4)] لە دەرەوەڕا بۆ ناوەوە بخوێنەوە: «بۆ هەر rێک لە 1 تا 3، [r * c for c in range(1, 5)] دروست بکە». هەمان وەک پێشتر، بەڵام وەک نووسراوە.

Tables from nested list comprehensionsخشتە بە دوو list comprehension یەک لە ناو یەکداcomprehension_table.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
rows, columns = 3, 4
zeros = [[0] * columns for row in range(rows)]
print(zeros)

table = [[r * c for c in range(1, 5)] for r in range(1, 4)]
for row in table:
    print(row)

Common mistakesهەڵە باوەکان

Two-dimensional lists: mistakes to avoid: ئەو هەڵانەی دەبێت لێیان دوور بیت
MistakeهەڵەWhat happensچی ڕوو دەداتFixچارەسەر
marks[1, 2]TypeError: a list takes one index at a timeTypeError: لە هەر جارێکدا یەک وەردەگرێتmarks[1][2]
marks[2][1] ↔ marks[1][2]row and column swapped: the wrong cell و گۆڕاونەتەوە: خانەی هەڵەrow index first, then column indexسەرەتا ، پاشان
marks[4][0] (4 rows)IndexError: rows are 0–3IndexError: ⁦0–3⁩ـنthe last row is marks[len(marks) - 1] or marks[-1]دوایین marks[len(marks) - 1] یان marks[-1]ـە
[[0] * 3] * 2one row shared twiceیەک کە دوو جار هاوبەشە[[0] * 3 for row in range(2)]
no print() after the inner loopprint() نییە دوای the whole table on one lineهەموو خشتەکە لەسەر یەک دێڕadd print() inside the outer loopprint() لە ناو زیاد بکە
matrix[row].append(v) before matrix.append([])matrix[row].append(v) پێش matrix.append([])IndexError: the row does not exist yetIndexError: هێشتا بوونی نییەappend the empty row firstسەرەتا بەتاڵەکە زیاد بکە

Exercisesڕاهێنانەکان

Exerciseڕاهێنان1. A distance table1. خشتەی دووری

Four places A, B, C and D have these (made-up) distances in km. Print the distance from B to C and from C to B, and check with nested loops that the table is a symmetric matrix: distances[i][j] == distances[j][i] for every i and j.

چوار شوێنی A، B، C و D ئەم دووریانەیان (دروستکراو) بە کیلۆمەتر هەیە. دووریی B بۆ C و C بۆ B چاپ بکە و بە بپشکنە کە خشتەکە : distances[i][j] == distances[j][i] بۆ هەموو i و jێک.

Hintئاماژە

distances[1][2] is row B, column C.

distances[1][2] Bـە، C.

Show answerوەڵامەکە پیشان بدە
ex1_distances.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
places = ["A", "B", "C", "D"]
distances = [            # made-up distances in km
    [0, 12, 30, 25],
    [12, 0, 18, 40],
    [30, 18, 0, 22],
    [25, 40, 22, 0],
]

print("B to C:", distances[1][2], "km")
print("C to B:", distances[2][1], "km")

symmetric = True
for i in range(len(distances)):
    for j in range(len(distances)):
        if distances[i][j] != distances[j][i]:
            symmetric = False
print("symmetric:", symmetric)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 12-16

Start by assuming "symmetric" and look for one pair that breaks it.

سەرەتا وا دابنێ کە خشتەکە و بەدوای یەک جووتدا بگەڕێ کە ئەمە تێک دەدات.

A distance table is always symmetric (A→B equals B→A), and its main diagonal distances[i][i] is 0 — the distance from a place to itself. The course's original table of seven cities has the same two properties. Mathematics calls such a table a symmetric matrix: see Symmetric tables in daily life.

خشتەی دووری هەمیشە (دووریی A بۆ B یەکسانە بە دووریی B بۆ A) و distances[i][i] سفرە — دووریی شوێنێک لە خۆیەوە. خشتە ڕەسەنەکەی کۆرس بۆ حەوت شار هەمان دوو تایبەتمەندیی هەیە. زیاتر لە خشتەی هاوجێ لە ژیانی ڕۆژانەدا.

Exerciseڕاهێنان2. Who sits there?2. کێ لەوێ دادەنیشێت؟

A small classroom has 2 rows of 4 seats, stored in seats. People count rows and seats from 1. Print the name in row 2, seat 3.

پۆلێکی بچووک 2 و هەر 4 کورسیی هەیە، لە seatsـدا هەڵگیراوە. خەڵک و کورسییەکان لە 1ـەوە دەژمێرن. ناوی 2، کورسیی 3 چاپ بکە.

Hintئاماژە

Subtract 1 from both numbers before you use them as indexes.

پێش ئەوەی وەک بەکاریان بهێنیت 1 لە هەردوو ژمارەکە کەم بکەرەوە.

Show answerوەڵامەکە پیشان بدە
ex2_seats.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
seats = [
    ["Aram", "Hana", "Soran", "Lana"],
    ["Shilan", "Rezan", "Dilan", "Karwan"],
]
row, seat = 2, 3                    # as people count: from 1
print(seats[row - 1][seat - 1])     # Python counts from 0
Exerciseڕاهێنان3. Corners of a grid3. گۆشەکانی تۆڕێک

Make a 4×64 \times 6 table of zeros without the * 4 trap, set its four corners to 1, and print it as rows.

خشتەیەکی 4×64 \times 6 لە سفر دروست بکە بەبێ داوی * 4، چوار گۆشەکەی بکە بە 1 و وەک چاپی بکە.

Show answerوەڵامەکە پیشان بدە
ex3_corners.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
grid = [[0] * 6 for row in range(4)]
grid[0][0] = grid[0][-1] = grid[-1][0] = grid[-1][-1] = 1
for row in grid:
    for value in row:
        print(value, end=" ")
    print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

-1 is the last row or the last column.

-1 دوایین یان دوایین .

Exerciseڕاهێنان4. A student record inside one list4. تۆماری خوێندکارێک لە ناو یەک لیستدا

One list keeps a student's record: the name, the student number, a list of five marks and the percentage: record = ["Shlan", 1024, [78, 85, 90, 66, 72], 78.2]. Print the percentage, the fifth mark, the highest mark and the student number. The name was typed wrong: change it to "Shilan".

یەک خوێندکارێک هەڵدەگرێت: ناو، ژمارەی خوێندکار، لە پێنج نمرە و : record = ["Shlan", 1024, [78, 85, 90, 66, 72], 78.2]. ، پێنجەم نمرە، بەرزترین نمرە و ژمارەی خوێندکار چاپ بکە. ناوەکە بە هەڵە نووسراوە: بیگۆڕە بۆ "Shilan".

Hintئاماژە

The marks list is item 2 of record, so the fifth mark is record[2][4].

نمرەکان 2ـی recordـە، بۆیە پێنجەم نمرە record[2][4]ـە.

Show answerوەڵامەکە پیشان بدە
ex4_record.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
record = ["Shlan", 1024, [78, 85, 90, 66, 72], 78.2]

print("percentage:", record[3])          # or record[-1]
print("fifth mark:", record[2][4])
print("highest mark:", max(record[2]))
print("student number:", record[1])
record[0] = "Shilan"           # the name was typed wrong
print(record)
print(sum(record[2]) / len(record[2]))   # check the percentage

Only the marks are a list inside the list, so only they need two indexes. max(record[2]) works because record[2] is an ordinary list of numbers, and the last line checks that 78.2 really is the average of the five marks. A whole class would be a list of such records — a table whose rows mix names and numbers. For records with many fields, a dictionary with named keys is often clearer: see Dictionaries.

تەنها نمرەکان لە ناو ، بۆیە تەنها ئەوان پێویستیان بە دوو هەیە. max(record[2]) کار دەکات چونکە record[2] ئاسایی ژمارەیە و دوایین دێڕ دەپشکنێت کە ⁦78.2⁩ بە ڕاستی تێکڕای پێنج نمرەکەیە. هەموو پۆلێک دەبێتە لەم — خشتەیەک کە ناو و ژمارە تێکەڵ دەکەن. بۆ زۆر بەش، بە ناوداری زۆرجار ڕوونترە: فەرهەنگەکان ببینە.

Quick checkپشکنینی خێراWhich line builds a 3×33 \times 3 table whose rows are separate lists?کام دێڕ خشتەیەکی 3×33 \times 3 دروست دەکات کە جیان؟

Summaryپوختە

  • A two-dimensional list is a list of rows; each row is a list. It stores a table or a matrix.
  • table[i] is row i; table[i][j] is the value in row i, column j. len(table) counts rows, len(table[0]) columns.
  • Fill a table with two loops: append([]) for each row, then table[row].append(value) — from int(input()) or random.randint.
  • Never build a table with [[0] * n] * m: all rows would be one list.
  • To print a table, end each row with print() after the inner loop.
  • لە ؛ هەر . خشتە یان هەڵدەگرێت.
  • table[i] iـە؛ table[i][j] ئەو کە لە i و jـدایە. len(table) دەژمێرێت، len(table[0]) .
  • خشتە بە دوو پڕ بکەرەوە: بۆ هەر append([])، پاشان table[row].append(value) — لە int(input()) یان random.randintـەوە.
  • هەرگیز خشتە بە [[0] * n] * m دروست مەکە: هەموو دەبنە یەک .
  • بۆ چاپکردنی خشتە، هەر بە print() دوای تەواو بکە.
Review questionsپرسیارەکانی پێداچوونەوە
What is len(marks[0]) for the marks table, and what does it mean?len(marks[0]) بۆ خشتەی نمرەکان چەندە و واتای چییە؟

4: the length of the first row, which is the number of columns (subjects).

4: درێژیی یەکەم ، کە ژمارەی (وانەکان).

Why does [[0] * 3] * 2 behave strangely?بۆچی [[0] * 3] * 2 سەیر ڕەفتار دەکات؟

* 2 copies the reference to the one inner list, so both "rows" are the same object.

* 2 یەک کۆپی دەکات، بۆیە هەردوو «» هەمان .

Next: totals by row and by column, the best row, sorting rows and passing tables to functions, in Processing two-dimensional lists.

داهاتوو: کۆی هەر و هەر ، باشترین ، ڕیزکردنی و ناردنی خشتە بۆ ، لە کارکردن لەسەر لیستە دووڕەهەندەکان.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

2D Lists & Nested Loops - Python - Tutorial 24Giraffe Academy · Englishئینگلیزی · 182K viewsبینین · 3.8K likesلایک

A grid of numbers as a list of rows, reaching one value with grid[row][column], and printing every value with a nested for loop.

خشتەیەکی ژمارە وەک لە ، گەیشتن بە یەک بە grid[row][column] و پیشاندانی هەموو بە ناوەکی.

Whole video (5:49): two-dimensional lists (0:11), then a nested for loop over the rows (3:28).

هەموو ڤیدیۆکە ⁦(5:49)⁩: ⁦(0:11)⁩، پاشان ناوەکی بەسەر ⁦(3:28)⁩.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Processing Two-Dimensional Listsکارکردن لەسەر لیستە دووڕەهەندەکان

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.