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، چاپکردنی خشتە بە و دروستکردنی خشتە بە دوو یەک لە ناو یەکدا.
What you will learnچی فێر دەبیت
- Store a table as a list of rows and say what
len(table)andlen(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] * mtrap. - 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.
زۆربەی ئەو لە دەوروبەرماندایە وەک خشتە دێت: نمرەی خوێندکاران لە وانەکاندا، خشتەی وانەکانی هەفتە، دووریی نێوان شارۆچکەکان، خانەکانی تەختەی یارییەک. خشتە و هەیە. لە وەک هەڵیدەگریت: کە خۆیان ، بۆ هەر .
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ـە بۆ دروستکردنی .
| Studentخوێندکار | Programming | Mathsبیرکاری | Englishئینگلیزی | Logicلۆجیک |
|---|---|---|---|---|
| Aram | 85 | 72 | 90 | 64 |
| Shilan | 78 | 95 | 70 | 88 |
| Karwan | 92 | 60 | 81 | 75 |
| Dilan | 66 | 84 | 77 | 93 |
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.
هەر دێڕێک : چوار نمرەی خوێندکارێک، هەمیشە بە هەمان ڕیزبەندیی وانەکان.
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ـەوە دەست پێ دەکات و «نهۆمەکان» پێویست ناکات هەمان ژمارەی دەرگایان هەبێت: بە درێژیی جیاواز ڕێگە پێدراون. زۆربەی خشتەکان یەکسانیان دەهێڵنەوە.
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]) — درێژیی یەکەم — ژمارەی .
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]))4 4 [78, 95, 70, 88] <class 'list'>
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ـی ئەو .
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])matrix[0] is [1, 2, 3, 4, 5] matrix[1] is [6, 7, 0, 0, 0] matrix[2] is [0, 1, 0, 0, 0] matrix[3] is [1, 0, 0, 0, 8] matrix[4] is [0, 0, 9, 0, 3] matrix[0][0] is 1 matrix[4][4] is 3
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.](/_astro/py-2d-lists-row-then-column.en.B971tbLH.gif)
![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.](/_astro/py-2d-lists-row-then-column.ckb.BxedfsGZ.gif)
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 ڕادەگرێت.
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 4Karwan, Maths: 60
after the re-check: [92, 65, 81, 75]
last student, last subject: 93
Traceback (most recent call last):
File "change_cell.py", line 12, in <module>
print(marks[4][0]) # there is no row 4
~~~~~^^^
IndexError: list index out of rangeExplain 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 بوونی نییە.
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) زیاد بکە. دوو ، یەکێکیان لە ناو ئەوی تردا: بەسەر دەڕوات و بەسەر ئێستاکەدا.
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)Enter the number of rows: 2 Enter the number of columns: 4 Enter an element and press Enter: 5 Enter an element and press Enter: 7 Enter an element and press Enter: 2 Enter an element and press Enter: 0 Enter an element and press Enter: 5 Enter an element and press Enter: 7 Enter an element and press Enter: 8 Enter an element and press Enter: 11 [[5, 7, 2, 0], [5, 7, 8, 11]]
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.
هەر دەچێتە ناو ئەو تازە زیاد کرا.
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 دەگەڕێنێتەوە؛ هەردوو سەرەکەی دەکرێت دەربکەون.
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)Enter the number of rows: 4 Enter the number of columns: 5 [[17, 72, 97, 8, 32], [15, 63, 97, 57, 60], [83, 48, 26, 12, 62], [3, 49, 55, 77, 97]]
The slide's run shows other numbers — every run is different unless you fix the seed.
جێبەجێکردنەکەی سلایدەکە ژمارەی تر پیشان دەدات — هەر جێبەجێکردنێک جیاوازە مەگەر seed جێگیر بکەیت.
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ـە. وەک دوو سفر چاپ دەبێت، بەڵام یەک بە دوو ناوەوە. * لەسەر ناو دووبارە دەکاتەوە؛ کۆپی ناکات. خانەیەک بگۆڕە و «تر»یش دەگۆڕێت.
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])[[5, 0, 0], [5, 0, 0]] True [[5, 0, 0], [0, 0, 0]] False
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
The inner
[0] * 3runs once; the outer* 2puts two references to that one row in the list.[0] * 3ـی ناوەوە یەکجار جێبەجێ دەبێت؛* 2ـی دەرەوە دوو بۆ هەمان یەک دەخاتە ناو .- Lineدێڕی 6
In a list comprehension (or a loop with
append),[0] * 3runs again for every row, so each row is a separate list.ئەگەر بەکار بهێنیت (یان بە
append)،[0] * 3بۆ هەر دووبارە جێبەجێ دەبێتەوە، بۆیە هەر جیایە.
* 2 repeats a reference; the comprehension builds new rows.* 2 دووبارە دەکاتەوە؛ نوێ دروست دەکات.CêITPrinting 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(...)))، یان ڕاستەوخۆ بەسەر و .
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=" ")1 2 3 4 5 6 7 8 9
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in matrix:
for value in row:
print(value, end=" ")1 2 3 4 5 6 7 8 9
Both print the nine numbers on one line — not yet a table.
هەردووکیان نۆ ژمارەکە لەسەر یەک دێڕ چاپ دەکەن — هێشتا خشتە نییە.
With field widths from f-strings (see Nested loops) the marks table gets headings and straight columns:
بە پانیی خانە لە f-stringـەوە (خولە ناوەکییەکان ببینە) خشتەی نمرەکان ناونیشان و ڕێک وەردەگرێت:
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()Prog Maths Eng Logic Aram 85 72 90 64 Shilan 78 95 70 88 Karwan 92 60 81 75 Dilan 66 84 77 93
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().
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)] دروست بکە». هەمان وەک پێشتر، بەڵام وەک نووسراوە.
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)[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] [1, 2, 3, 4] [2, 4, 6, 8] [3, 6, 9, 12]
Common mistakesهەڵە باوەکان
| 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] * 2 | one 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ڕاهێنانەکان
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وەڵامەکە پیشان بدە
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)B to C: 18 km C to B: 18 km symmetric: True
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] سفرە — دووریی شوێنێک لە خۆیەوە. خشتە ڕەسەنەکەی کۆرس بۆ حەوت شار هەمان دوو تایبەتمەندیی هەیە. زیاتر لە خشتەی هاوجێ لە ژیانی ڕۆژانەدا.
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وەڵامەکە پیشان بدە
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 0Dilan
Make a table of zeros without the * 4 trap, set its four corners to 1, and print it as rows.
خشتەیەکی لە سفر دروست بکە بەبێ داوی * 4، چوار گۆشەکەی بکە بە 1 و وەک چاپی بکە.
Show answerوەڵامەکە پیشان بدە
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()1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
-1is the last row or the last column.-1دوایین یان دوایین .
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وەڵامەکە پیشان بدە
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 percentagepercentage: 78.2 fifth mark: 72 highest mark: 90 student number: 1024 ['Shilan', 1024, [78, 85, 90, 66, 72], 78.2] 78.2
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 بە ڕاستی تێکڕای پێنج نمرەکەیە. هەموو پۆلێک دەبێتە لەم — خشتەیەک کە ناو و ژمارە تێکەڵ دەکەن. بۆ زۆر بەش، بە ناوداری زۆرجار ڕوونترە: فەرهەنگەکان ببینە.
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 rowi;table[i][j]is the value in rowi, columnj.len(table)counts rows,len(table[0])columns.- Fill a table with two loops:
append([])for each row, thentable[row].append(value)— fromint(input())orrandom.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()دوای تەواو بکە.
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
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زاراوە سەرەکییەکان
Processing Two-Dimensional Listsکارکردن لەسەر لیستە دووڕەهەندەکان
Related lecturesوانە پەیوەندیدارەکان
- The for Loop and range()خولی for و
range() - Processing Two-Dimensional Listsکارکردن لەسەر لیستە دووڕەهەندەکان
- The Python You Need for the AI Labsئەو Pythonـەی بۆ تاقیگەکانی ژیریی دەستکرد پێویستە
- Building Tic-Tac-Toe in Pythonدروستکردنی یاریی ئێکسوئۆ بە Python
- Reading and Writing Filesخوێندنەوە و نووسینی فایلەکان
- Matrix Notation and Special Matricesنووسینی ماتریکس و جۆرە تایبەتەکانی
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 5.1.4: Nested list comprehensions (a matrix as a list of lists)
- Python FAQ: How do I create a multidimensional list? (the [[None] * 2] * 3 trap)
- Python Standard Library: random — randint(a, b) includes both end points
- Python tutorial 7.1: Fancier output formatting (field widths)
- Liang, Introduction to Programming Using Python — table of contents (chapter 11, Multidimensional lists)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.