PythonPython · Chapterبەش 9 · Lectureوانە 9.7
Processing Two-Dimensional Listsکارکردن لەسەر لیستە دووڕەهەندەکان
Working with tables: the total of all values, totals row by row and column by column (and the slide's misplaced print), the row with the largest sum, sorting rows lexicographically or by a computed key, and passing tables to and from functions — with the shared-rows trap of shallow copies.
کارکردن لەگەڵ خشتە: کۆی هەموو ، کۆ بە و بە (و printـە هەڵە دانراوەکەی سلایدەکە)، ئەو گەورەترین کۆی هەیە، بە یان بەپێی کلیلێکی حیسابکراو و ناردن و گەڕاندنەوەی خشتە بۆ و لێیانەوە — لەگەڵ داوی هاوبەشەکانی .
What you will learnچی فێر دەبیت
- Add up a whole table, each row and each column with nested loops, putting the accumulator and the
printin the right place. - Find the row with the largest sum and report its index.
- Explain how
sort()orders rows, and rank rows by a computed total withkey=. - Write functions that read, return and process tables, and protect a table with a deep copy.
- بە هەموو خشتەکە، هەر و هەر کۆ بکەیتەوە و و
printلە شوێنی دروستدا دابنێیت. - ئەو بدۆزیتەوە کە گەورەترین کۆی هەیە و ڕابگەیەنیت.
- ڕوون بکەیتەوە
sort()چۆن ڕیز دەکات و بەkey=بەپێی کۆیەکی حیسابکراو ڕیز بکەیت. - بنووسیت کە خشتە دەخوێننەوە، دەیگەڕێننەوە و کاری لەسەر دەکەن و خشتەیەک بە بپارێزیت.
Adding up the whole tableکۆکردنەوەی هەموو خشتەکە
Processing a table almost always means visiting its values with two loops. The simplest job is the total of every value: start an accumulator total at 0, and let the inner loop add each value of each row to it.
کارکردن لەسەر خشتە نزیکەی هەمیشە واتە سەردانی بە دوو . سادەترین کار کۆی هەموو : بە ناوی total لە 0ـەوە دەست پێ بکە و با هەر هەر بۆی زیاد بکات.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # assume a list is given
total = 0
for row in matrix:
for value in row:
total += value
print("Total is", total) # print the totalTotal is 45
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The accumulator starts at 0, before both loops.
پێش هەردوو لە 0ـەوە دەست پێ دەکات.
- Lineدێڕی 4
On the slide this inner
forline is indented by only one space. Python accepts it, but PEP 8 asks for 4 spaces at every level.لە سلایدەکەدا ئەم دێڕەی
forـی ناوەوە تەنها بە یەک بۆشایی چووەتە ناوەوە. وەریدەگرێت، بەڵام PEP 8 بۆ هەموو ئاستێک 4 بۆشایی دەخوازێت.
The inner loop can also be replaced by sum(row), which adds one whole row. Here is the same idea on the marks table from the previous lesson:
دەکرێت بە sum(row)ـیش جێگە بگیرێتەوە، کە تەواو کۆ دەکاتەوە. ئەمە هەمان بیرۆکەیە لەسەر خشتەی نمرەکانی وانەی پێشوو:
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
]
total = 0
for row in marks:
total += sum(row) # sum() adds one whole row at a time
print("All marks together:", total)
print("Average mark:", total / (len(marks) * len(marks[0])))All marks together: 1270 Average mark: 79.375
Row by row: one total for each rowڕیز بە ڕیز: بۆ هەر ڕیزێک کۆیەک
Often you need a result per row — each student's total, each shop's weekly sales. Then the total must start again for every row: either with sum(row), or with total = 0 placed inside the outer loop, before the inner one.
زۆرجار ئەنجامێکت بۆ هەر پێویستە — کۆی هەر خوێندکارێک، فرۆشی هەفتانەی هەر دوکانێک. ئەوکات کۆکە دەبێت بۆ هەر لە سەرەتاوە دەست پێ بکاتەوە: یان بە sum(row)، یان بە total = 0 کە لە ناو و پێش دادەنرێت.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row in range(len(matrix)):
print("Sum for row", row, "is", sum(matrix[row]))Sum for row 0 is 6 Sum for row 1 is 15 Sum for row 2 is 24
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"]
for i in range(len(marks)):
total = sum(marks[i])
average = total / len(marks[i])
print(f"{names[i]:7} total {total} average {average:.2f}")Aram total 311 average 77.75 Shilan total 331 average 82.75 Karwan total 308 average 77.00 Dilan total 320 average 80.00
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 12
:.2fprints the average with two decimal places.:.2fتێکڕاکە بە دوو ژمارە دوای خاڵ چاپ دەکات.
Column by columnستوون بە ستوون
A column is not stored as one list — its values are spread over all the rows. So for a column total the loops swap roles: the outer loop walks the columns, and the inner loop goes down the rows, adding matrix[row][column]. The total starts at 0 again for every column.
وەک یەک هەڵناگیرێت — بەسەر هەموو بڵاو بوونەتەوە. بۆیە بۆ کۆی ڕۆڵ دەگۆڕنەوە: بەسەر دەڕوات و بە دادەبەزێت و matrix[row][column] زیاد دەکات. کۆکە بۆ هەر دووبارە لە 0ـەوە دەست پێ دەکات.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # assume a list is given
for column in range(len(matrix[0])):
total = 0
for row in range(len(matrix)):
total += matrix[row][column]
print("Sum for last column is", total)Sum for last column is 18
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
This
printis outside the column loop, so it runs once, after the last column.ئەم
printـە لە دەرەوەی ، بۆیە یەکجار جێبەجێ دەبێت، دوای دوایین .


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
]
subjects = ["Programming", "Maths", "English", "Logic"]
for column in range(len(subjects)):
total = 0
for row in range(len(marks)):
total += marks[row][column]
print(f"{subjects[column]:12} total {total} average {total / len(marks):.2f}")Programming total 321 average 80.25 Maths total 311 average 77.75 English total 318 average 79.50 Logic total 320 average 80.00
The row with the largest sumئەو ڕیزەی گەورەترین کۆی هەیە
This combines two ideas you know: the row sum and the "largest so far" scan. Keep the largest sum in maxRow and its row number in indexOfMaxRow. Start with row 0, then compare every other row's sum and update both variables when a bigger one appears.
ئەمە دوو بیرۆکەی ناسراو پێکەوە دەبەستێت: کۆی و گەڕانی «گەورەترین تا ئێستا». گەورەترین کۆ لە maxRowـدا و ژمارەی لە indexOfMaxRowـدا ڕابگرە. بە 0 دەست پێ بکە، پاشان کۆی هەموو تر بەراورد بکە و کاتێک کۆیەکی گەورەتر دەرکەوت هەردوو نوێ بکەرەوە.
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # assume a list is given
maxRow = sum(matrix[0]) # get sum of the first row in maxRow
indexOfMaxRow = 0
for row in range(1, len(matrix)):
if sum(matrix[row]) > maxRow:
maxRow = sum(matrix[row])
indexOfMaxRow = row
print("Row with the index ", indexOfMaxRow, "has the maximum sum of", maxRow)Row with the index 2 has the maximum sum of 24
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
Both variables start with row 0, so they always exist — unlike the
min_positionbug of the sorting lesson.هەردوو بە 0 دەست پێ دەکەن، بۆیە هەمیشە بوونیان هەیە — بە پێچەوانەی هەڵەی
min_positionـی وانەی .- Lineدێڕی 5
The loop starts at row 1: row 0 is already the "best so far".
لە 1ـەوە دەست پێ دەکات: 0 پێشتر «باشترین تا ئێستا»یە.
The same scan finds the best student in the marks table. (In new code use snake_case names such as best_total, as PEP 8 suggests.) The last two lines show a shortcut from sorting with key=: max(marks, key=sum) returns the row with the largest sum.
هەمان گەڕان باشترین خوێندکار لە خشتەی نمرەکاندا دەدۆزێتەوە. (لە کۆدی نوێدا ناوی snake_case وەک best_total بەکار بهێنە، وەک PEP 8 پێشنیاری دەکات.) دوو دێڕی کۆتایی ڕێگەیەکی کورت لە ڕیزکردن بە key= پیشان دەدەن: max(marks, key=sum) ئەو دەگەڕێنێتەوە کە گەورەترین کۆی هەیە.
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"]
best_total = sum(marks[0])
best_index = 0
for row in range(1, len(marks)):
row_total = sum(marks[row])
if row_total > best_total:
best_total = row_total
best_index = row
print("Best student:", names[best_index], "with", best_total)
# the same with max() and key= (see the sorting lesson)
best_row = max(marks, key=sum)
print(best_row, "is row", marks.index(best_row))Best student: Shilan with 331 [78, 95, 70, 88] is row 1
Sorting the rows of a tableڕیزکردنی ڕیزەکانی خشتەیەک
sort() works on a two-dimensional list too. It compares whole rows the way a dictionary compares words: first by their first elements; when those are equal, by the second elements; when those are equal too, by the third, and so on. This is called lexicographic order — the same rule that < and > use on two lists (< and >: dictionary order for lists).
sort() لەسەر کار دەکات. تەواو بەو شێوەیە بەراورد دەکات کە فەرهەنگ وشەکان بەراورد دەکات: سەرەتا بەپێی یەکەم ؛ کاتێک ئەوانە یەکسان بن، بەپێی دووەمەکان؛ کاتێک ئەوانیش یەکسان بن، بەپێی سێیەمەکان و بەم شێوەیە. بەمە دەوترێت — هەمان یاسا کە < و > لەسەر دوو بەکاری دەهێنن (< و >: ڕیزبەندیی فەرهەنگی بۆ لیستەکان).
points = [[4, 2], [1, 7], [4, 5], [1, 2], [1, 1], [4, 1]]
points.sort()
print(points)[[1, 1], [1, 2], [1, 7], [4, 1], [4, 2], [4, 5]]
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
[1, 7],[1, 2]and[1, 1]share the first element 1, so the second element decides their order: 1, 2, 7.[1, 7]،[1, 2]و[1, 1]هەمان یەکەم 1ـیان هەیە، بۆیە دووەم ڕیزبەندییان دیاری دەکات: 1، 2، 7.
To rank students by their total, the normal order is not enough — it would sort by name. Give sort a key function that computes the total of a row, and reverse=True for the largest first. Keeping the name inside each row means it moves together with the marks.
بۆ خوێندکاران بەپێی کۆیان، ڕیزبەندییە ئاساییەکە بەس نییە — بەپێی ناو ڕیز دەکات. key بدە بە sort کە کۆی حیساب بکات و reverse=True بۆ ئەوەی گەورەترین یەکەم بێت. هێشتنەوەی ناوەکە لە ناو هەر وا دەکات لەگەڵ نمرەکاندا بجووڵێت.
students = [
["Aram", 85, 72, 90, 64],
["Shilan", 78, 95, 70, 88],
["Karwan", 92, 60, 81, 75],
["Dilan", 66, 84, 77, 93],
]
def total(row):
return sum(row[1:]) # skip the name in position 0
students.sort(key=total, reverse=True)
for place, row in enumerate(students, start=1):
print(place, row[0], total(row))1 Shilan 331 2 Dilan 320 3 Aram 311 4 Karwan 308
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8-9
row[1:]is a slice without the name, sosumonly adds the marks.row[1:]بە بەبێ ناوەکە دەدات، بۆیەsumتەنها نمرەکان کۆ دەکاتەوە.- Lineدێڕی 12
enumerate(…, start=1)numbers the places from 1.enumerate(…, start=1)پلەکان لە 1ـەوە ژمارە دەکات.
Passing tables to functionsناردنی خشتە بۆ کردارەکان
A two-dimensional list goes into a function exactly like a one-dimensional one: the function receives a reference to the same table. A function can also build a table and return it. Splitting the work into functions keeps each part short: get_matrix reads a table, accumulate adds it up and main connects them.
ڕێک وەک یەکڕەهەند دەچێتە ناو : بۆ هەمان خشتە وەردەگرێت. دەتوانێت خشتەیەکیش دروست بکات و بیگەڕێنێتەوە. دابەشکردنی کارەکە بۆ چەند هەر بەشێک کورت دەهێڵێتەوە: get_matrix خشتەیەک دەخوێنێتەوە، accumulate کۆی دەکاتەوە و main پێکیانەوە دەبەستێت.
def get_matrix():
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 a value and press Enter: "))
matrix[row].append(value)
return matrix
def accumulate(m):
total = 0
for row in m:
total += sum(row)
return total
def main():
m = get_matrix() # get a list
print(m)
print("\nSum of all elements is", accumulate(m)) # display the sum
main() # invoke main functionEnter the number of rows: 2 Enter the number of columns: 2 Enter a value and press Enter: 1 Enter a value and press Enter: 2 Enter a value and press Enter: 3 Enter a value and press Enter: 4 [[1, 2], [3, 4]] Sum of all elements is 10
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
The function returns the table it built;
mainkeeps it inm.ئەو خشتەیەی دروستی کرد دەگەڕێنێتەوە؛
mainلەmـدا هەڵیدەگرێت.- Lineدێڕی 12
accumulatereceives a reference to the same table and only reads it.accumulateبۆ هەمان خشتە وەردەگرێت و تەنها دەیخوێنێتەوە.- Lineدێڕی 21
\nat the start prints an empty line first.\nلە سەرەتادا یەکەمجار دێڕێکی بەتاڵ چاپ دەکات.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
total = 0 before the loops when you want one total per row/columntotal = 0 پێش کاتێک بۆ هەر / کۆیەکت دەوێت | the totals run on into each otherکۆکان تێکەڵی یەکتر دەبن | reset total = 0 inside the outer looptotal = 0 لە ناو دووبارە دابنێ |
print after both loopsprint دوای هەردوو | only the last column's total is shownتەنها کۆی دوایین پیشان دەدرێت | print inside the outer loop, after the inner loopلە ناو ، دوای چاپ بکە |
matrix[column][row] | rows and columns swapped (or IndexError if the table is not square) و گۆڕاونەتەوە (یان IndexError ئەگەر خشتەکە چوارگۆشە نەبێت) | matrix[row][column] |
range(len(matrix)) for the columnsrange(len(matrix)) بۆ | wrong count when the table is not squareژمارەی هەڵە کاتێک خشتەکە چوارگۆشە نییە | range(len(matrix[0])) |
| sorting rows that start with a name, expecting a ranking by marks ئەو بە ناو دەست پێ دەکەن، بە چاوەڕوانیی ڕیزبەندی بەپێی نمرە | rows are sorted by name بەپێی ناو ڕیز دەکرێن | sort(key=…) with a function that computes the totalsort(key=…) لەگەڵ کە کۆکە حیساب دەکات |
backup = table.copy() | the rows are still shared هێشتا هاوبەشن | copy.deepcopy(table) |
Exercisesڕاهێنانەکان
Using the marks table, find the subject with the lowest average.
بە بەکارهێنانی خشتەی نمرەکان، ئەو وانەیە بدۆزەرەوە کە نزمترین تێکڕای هەیە.
Hintئاماژە
Column averages + the "smallest so far" scan.
تێکڕای + گەڕانی «بچووکترین تا ئێستا».
Show answerوەڵامەکە پیشان بدە
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
]
subjects = ["Programming", "Maths", "English", "Logic"]
lowest_index = 0
lowest_average = sum(row[0] for row in marks) / len(marks)
for column in range(1, len(subjects)):
average = sum(row[column] for row in marks) / len(marks)
if average < lowest_average:
lowest_average = average
lowest_index = column
print("Hardest subject:", subjects[lowest_index], lowest_average)Hardest subject: Maths 77.75
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
sum(row[0] for row in marks)adds column 0 down all rows (a short form of the inner loop).sum(row[0] for row in marks)0 بە هەموو کۆ دەکاتەوە (شێوەیەکی کورتی ).
Find who has the highest Maths mark. The column is fixed (1); scan the rows.
بیدۆزەرەوە کێ بەرزترین نمرەی بیرکاریی هەیە. جێگیرە (1)؛ بەسەر بگەڕێ.
Show answerوەڵامەکە پیشان بدە
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"]
column = 1 # Maths
best_row = 0
for row in range(1, len(marks)):
if marks[row][column] > marks[best_row][column]:
best_row = row
print("Best in Maths:", names[best_row], marks[best_row][column])Best in Maths: Shilan 95
In a square matrix the main diagonal is matrix[0][0], matrix[1][1], matrix[2][2] … Add it up with one loop.
لە بریتییە لە matrix[0][0]، matrix[1][1]، matrix[2][2] … بە یەک کۆی بکەرەوە.
Hintئاماژە
On the diagonal the row index and the column index are equal.
لەسەر و یەکسانن.
Show answerوەڵامەکە پیشان بدە
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
diagonal = 0
for i in range(len(matrix)):
diagonal += matrix[i][i]
print("Diagonal sum:", diagonal)Diagonal sum: 15
Mathematics has a name for this sum: the trace of the matrix. See The trace of a square matrix.
بیرکاری ناوێکی بۆ ئەم کۆیە هەیە: . کۆی تیرەی ماتریکسێکی چوارگۆشەیی ببینە.
Three shops record their sales for five days in sales. Print each shop's weekly total (row sums) and each day's total over all shops (column sums).
سێ دوکان فرۆشی پێنج ڕۆژیان لە salesـدا تۆمار دەکەن. کۆی هەفتانەی هەر دوکانێک (کۆی ) و کۆی هەر ڕۆژێک بۆ هەموو دوکانەکان (کۆی ) چاپ بکە.
Show answerوەڵامەکە پیشان بدە
sales = [ # 3 shops × 5 days (made-up numbers, in thousands of dinars)
[120, 95, 130, 110, 150],
[80, 100, 90, 85, 120],
[140, 135, 160, 150, 170],
]
for shop in range(len(sales)):
print("Shop", shop + 1, "week total:", sum(sales[shop]))
for day in range(len(sales[0])):
day_total = 0
for shop in range(len(sales)):
day_total += sales[shop][day]
print("Day", day + 1, "all shops:", day_total)Shop 1 week total: 605 Shop 2 week total: 475 Shop 3 week total: 755 Day 1 all shops: 340 Day 2 all shops: 330 Day 3 all shops: 380 Day 4 all shops: 345 Day 5 all shops: 440
In a Minesweeper-style game a board hides 6 bombs, and every safe cell shows how many of its neighbours (up to 8) are bombs. Store the board as a two-dimensional list (1 = bomb, 0 = safe) and print the board of numbers, with * for a bomb.
لە یارییەکی وەک Minesweeper، تەختەیەکی شەش بۆمبی تێدا شاراوەتەوە و هەر خانەیەکی سەلامەت پیشان دەدات چەند لە خانە دراوسێکانی (تا 8 خانە) بۆمبن. تەختەکە وەک هەڵبگرە (1 = بۆمب، 0 = سەلامەت) و تەختەی ژمارەکان چاپ بکە، بە * بۆ بۆمب.
Hintئاماژە
The neighbours of the cell in row r, column c lie in rows r - 1 to r + 1 and columns c - 1 to c + 1. Check that a neighbour is inside the board first: grid[-1] does not fail — it is the last row — so it would quietly count the wrong cells.
دراوسێکانی خانەی r و c لە r - 1 تا r + 1 و c - 1 تا c + 1ـدان. سەرەتا بپشکنە کە دراوسێکە لە ناو تەختەکەدایە: grid[-1] هەڵە نادات — دوایین — بۆیە بە بێدەنگی خانەی هەڵە دەژمێرێت.
Show answerوەڵامەکە پیشان بدە
grid = [ # 1 = bomb, 0 = safe (6 bombs)
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1],
]
size = len(grid)
def bombs_around(r, c):
count = 0
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):
rr, cc = r + dr, c + dc
if 0 <= rr < size and 0 <= cc < size:
count += grid[rr][cc]
return count
for r in range(size):
line = []
for c in range(size):
if grid[r][c] == 1:
line.append("*")
else:
line.append(str(bombs_around(r, c)))
print(" ".join(line))1 * 2 1 1 0 2 2 2 * 1 0 * 1 1 2 2 1 2 2 1 1 * 1 1 * 1 1 2 2 1 1 1 0 1 *
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 13-14
Two small loops visit the block around the cell (the cell itself is 0, so it adds nothing).
دوو بچووک سەردانی ئەو چوارگۆشە ـیە دەکەن کە دەوری خانەکەی داوە (خودی خانەکە 0ـە، بۆیە هیچ زیاد ناکات).
- Lineدێڕی 16
Only neighbours inside the board are counted.
تەنها ئەو دراوسێیانە دەژمێردرێن کە لە ناو تەختەکەدان.
0 <= rr < size keeps the search on the board. Without it, a cell in row 0 would look at grid[-1], the bottom row (Which indexes are allowed?). A playable game would also read the player's row and column safely, as in A safe input function.
0 <= rr < size گەڕانەکە لە ناو تەختەکەدا دەهێڵێتەوە. بەبێ ئەو، خانەیەک لە 0 سەیری grid[-1] دەکات، واتە خوارەوە (کام ئیندێکسەکان ڕێگەپێدراون؟). یارییەکی تەواو هەروەها و یاریزانەکە بە سەلامەتی دەخوێنێتەوە، وەک لە کردارێکی نێوخستەی سەلامەت: get_int().
Summaryپوختە
- Whole table: one accumulator before both loops (or
sum(row)for each row). - Per row: reset the total for every row —
sum(table[row]). - Per column: outer loop over columns, inner loop over rows,
table[row][column]; print inside the outer loop. - Largest row: start with row 0 as "best so far" and update the sum and the index together.
sort()orders rows lexicographically; usekey=to sort by a computed value such as a total.- Tables are passed to and returned from functions by reference; use
copy.deepcopy()to keep an independent copy.
- هەموو خشتەکە: یەک پێش هەردوو (یان
sum(row)بۆ هەر ). - بۆ هەر : کۆکە بۆ هەر سفر بکەرەوە —
sum(table[row]). - بۆ هەر : بەسەر ، بەسەر ،
table[row][column]؛ لە ناو چاپ بکە. - گەورەترین : بە 0 وەک «باشترین تا ئێستا» دەست پێ بکە و کۆ و پێکەوە نوێ بکەرەوە.
sort()بە ڕیز دەکات؛ بۆ بەپێی حیسابکراو وەک کۆkey=بەکار بهێنە.- خشتە بە دەنێردرێت بۆ و لێیانەوە دەگەڕێتەوە؛ بۆ کۆپییەکی سەربەخۆ
copy.deepcopy()بەکار بهێنە.
Why does the slide's column program print only one line?بۆچی بەرنامەکەی ستوونەکانی سلایدەکە تەنها یەک دێڕ چاپ دەکات؟
Its print is after the column loop, so it runs once, with the last column's total. Indent it into the column loop.
printـەکەی دوای ، بۆیە یەکجار جێبەجێ دەبێت، بە کۆی دوایین . بیبە ناو .
How does [[4, 2], [1, 7], [4, 1]].sort() decide the order?[[4, 2], [1, 7], [4, 1]].sort() چۆن ڕیزبەندییەکە دیاری دەکات؟
By the first elements (1 before 4), and for equal first elements by the second ones: [[1, 7], [4, 1], [4, 2]].
بەپێی یەکەم (1 پێش 4)؛ بۆ یەکەمی یەکسانیش بەپێی دووەمەکان: [[1, 7], [4, 1], [4, 2]].
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Tuples, Sets and Dictionariesتاپڵەکان، کۆمەڵەکان و فەرهەنگەکان
Related lecturesوانە پەیوەندیدارەکان
- Lists, References, Copies and Functionsلیستەکان، ئاماژەکان، کۆپی و کردارەکان
- Nested Loops and Patternsخولە ناوەکییەکان و کێشانی شێوە
- Defining and Calling Functionsناساندن و بانگکردنی کردارەکان
- Matrix Notation and Special Matricesنووسینی ماتریکس و جۆرە تایبەتەکانی
- Adding, Subtracting and Scaling Matricesکۆکردنەوە، لێدەرکردن و لێکدانی ماتریکسەکان لە ژمارە
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Python tutorial 5.8: Comparing sequences and other types (lexicographical ordering)
- Python Sorting Techniques (HOWTO): key functions and reverse
- Python Standard Library: copy — shallow and deep copy operations
- Python built-in functions: sum(), max(), enumerate()
- 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 ن.